思路是染色,WA 40pts MnZn求调
查看原帖
思路是染色,WA 40pts MnZn求调
648772
Liyuqiao11楼主2023/11/30 21:15
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
#define int long long
vector<pair<int,int> > G[N];
bool flag;
int c,t,n,m,last[N],bz[N],col[N],fa[N],siz[N],tot;
void dfs(int x){
	fa[x]=tot;
	siz[tot]++;
	for(int i=0;i<G[x].size();i++){
		int v=G[x][i].first,w=G[x][i].second;
		if(col[v]!=0){
			if(w==0){
				if(col[v]!=col[x]){
					flag=false;	
				}
			}
			if(w==1){
				if(col[v]==col[x]){
					flag=false;
				}
			}
		}
		else{
			if(w==0) col[v]=col[x];
			if(w==1){
				if(col[x]==1){
					col[v]=1;
				}
				if(col[x]==2){
					col[v]=3;
				}
				if(col[x]==3){
					col[v]=2;
				}
			}
			dfs(v);
		}
	}
}
signed main(){
	cin>>c>>t;
	while(t--){
		int ans=0;
		tot=0;
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			siz[i]=0;
			col[i]=0;
			last[i]=0;
			bz[i]=0;
			G[i].clear();
		}
		for(int i=1;i<=m;i++){
			char op;
			int x,y;
			cin>>op;
			if(op=='U'||op=='T'||op=='F'){
				cin>>x;
				if(op=='U') last[x]=1;
				if(op=='T') last[x]=2;
				if(op=='F') last[x]=3; 
				bz[x]=-1;
			}
			else{
				cin>>x>>y;
				last[x]=y+3;
				if(op=='-') bz[x]=2;
				else bz[x]=1; 
			}
		}
		for(int i=1;i<=n;i++){
			if(bz[i]==-1){
				col[i]=last[i];
			}
			if(bz[i]==1){
				G[last[i]-3].push_back(make_pair(i,0));
				G[i].push_back(make_pair(last[i]-3,0));
			}
			if(bz[i]==2){
				G[last[i]-3].push_back(make_pair(i,1));
				G[i].push_back(make_pair(last[i]-3,1));
			}
		}
		for(int i=1;i<=n;i++){
			if(bz[i]==-1){
				tot++;
				dfs(i);
				if(col[i]==1){
					ans+=siz[tot];
				}
			}
		}
		for(int i=1;i<=n;i++){
			if(col[i]==0){
				tot++;
				col[i]=2;
				flag=true;
				dfs(i);
				if(!flag){
					ans+=siz[tot];
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
2023/11/30 21:15
加载中...