社区讨论

求大佬帮忙卡常QAQ

P3921小学数学题参与者 4已保存回复 9

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
9 条
当前快照
1 份
快照标识符
@mihk91p1
此快照首次捕获于
2025/11/27 23:00
3 个月前
此快照最后确认于
2025/11/28 14:45
3 个月前
查看原帖
#18 1.48s,求大佬看看有没有常数可以卡 qwq
CPP
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=(1<<15)+5,inf=0x3f3f3f3f,maxm=55;
int n,m1,m2,r,f[maxn],g[maxn],a1[maxm],b1[maxm],a2[maxm],b2[maxm],c2[maxm];
vector<int> e;
bool vis[maxn],pass[maxn];
queue<int> q;
int count(int x)
{
	int res=0;
	while(x)
	{
		res++;
		x-=(x&-x);
	}
	return res;
}
bool check(int j)
{
	for(int i=1;i<=m1;i++)
	{
		if(((j>>(a1[i]-1))&1)!=((j>>(b1[i]-1))&1)) return 0;
	}
	for(int i=1;i<=m2;i++)
	{
		int u=(j>>(a2[i]-1))&1,v=(j>>(b2[i]-1))&1,w=(j>>(c2[i]-1))&1;
		if((u^v)&&v==w) return 0;
	}
	return 1;
}
void bfs()
{
	memset(f,0x3f,sizeof(f));
	f[0]=0;
	g[0]=1;
	vis[0]=1;
	q.push(0);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(auto v:e)
		{
			if(!pass[u^v]) continue;
			if(!vis[u^v])
			{
				f[u^v]=f[u]+1;
				vis[u^v]=1;
				q.push(u^v);
			}
			if(f[u]+1==f[u^v]) g[u^v]+=g[u];
		}
	}
}
signed main()
{
//	freopen("test.in","r",stdin);
//	freopen("test.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m1>>m2>>r;
	for(int i=1;i<=m1;i++) cin>>a1[i]>>b1[i];
	for(int i=1;i<=m2;i++) cin>>a2[i]>>b2[i]>>c2[i];
	for(int i=0;i<(1<<n);i++)
	{
		if(count(i)<=r) e.push_back(i);
		if(check(i)) pass[i]=1;
	}
	bfs();
	if(f[(1<<n)-1]!=inf) cout<<f[(1<<n)-1]<<" "<<g[(1<<n)-1];
	else cout<<"-1 0";
	return 0;
}

回复

9 条回复,欢迎继续交流。

正在加载回复...