CF1715D 2+ doors

就像位运算要从大到小贪心考虑一样,字典序也必须从第一位到末位逐位贪心考虑!

考虑按位枚举,再对于限制条件分类,对于一个点想要为 0,显然跟它所限制的都要能够满足,且不能影响之前的决策!保证无后效性捏。

赛时冲太久 2-SAT 了,但其实这题分析出来一条限制边 2 端至少要有 1 个 1,然后再从前到后枚举每一个位贪心考虑即可。压根不用对一个点拆成 2 个,因为这样的话并不是很好能表示一个点的最终状态。

#include <bits/stdc++.h>
//#define int long long
#define pb push_back
using namespace std;
const int N=(int)(2e5+5);
struct node {
	int x,y,z;
}a[N];
vector<int>g[N];
int n,q,d[N],ans[N],nw;

signed main() {
	cin.tie(0); ios::sync_with_stdio(false);
	cin>>n>>q;
	for(int i=1;i<=q;i++) {
		cin>>a[i].x>>a[i].y>>a[i].z;
	}
	for(int i=0;i<=29;i++) {
		nw=i;
		for(int j=1;j<=n;j++) d[j]=-1,g[j].clear();
		for(int j=1;j<=q;j++) {
			if((a[j].z>>i)&1) {
				if(a[j].x==a[j].y) {
					d[a[j].x]=1; continue ;
				}
				g[a[j].x].pb(a[j].y);
				g[a[j].y].pb(a[j].x);
			} else {
				d[a[j].x]=d[a[j].y]=0;
			}
		}
		for(int x=1;x<=n;x++) {
			if(d[x]==-1) {
				bool fl=1;
				for(int y:g[x]) {
					if(d[y]==0) {
						fl=0;
					}
				}
				if(fl) {
					d[x]=0;
					for(int y:g[x]) {
						d[y]=1;
					}
				} else {
					d[x]=1;
				}
			}
		}
		for(int j=1;j<=n;j++) ans[j]+=d[j]*(1<<i);
	}
	for(int i=1;i<=n;i++) cout<<ans[i]<<' ';
	return 0;
} 
posted @ 2022-08-24 18:57  FxorG  阅读(50)  评论(0编辑  收藏  举报