10分求条 AC on#3(其他帖子看过了
查看原帖
10分求条 AC on#3(其他帖子看过了
1259477
qiuxiangyu2010楼主2025/1/23 10:57
#include<bits/stdc++.h>
using namespace std;
int n,m;
int cnt,head[2000010];
int dfn[2000010],low[2000010],id,st[2000010],top,inst[2000010],tot,qiang[2000010];
struct E{
	int to,ne;
}e[4000010];
void add(int u,int v){
	cnt++;
	e[cnt].to=v;
	e[cnt].ne=head[u];
	head[u]=cnt;
}
void tj(int x){
	dfn[x]=low[x]=++id;
	st[++top]=x;
	inst[x]=1;
	for(int i=head[x];i;i=e[i].ne){
		int to=e[i].to;
		if(!dfn[to]){
			tj(to);
			low[x]=min(low[to],low[x]);
		}else if(inst[to]){
			low[x]=min(low[x],dfn[to]);
		}
	}
	if(low[x]==dfn[x]){
		int t=0;
		tot++;
		while(t!=x){
			t=st[top--];
			inst[t]=0;
			qiang[t]=tot;
		}
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int u,v,a,b;
		scanf("%d%d%d%d",&u,&a,&v,&b);
		if(a==0&&b==0){
			add(u+n,v);
			add(v+n,u);
		}
		if(a==1&&b==0){
			add(u,v);
			add(v+n,u+n);
		}
		if(b==0&&a==1){
			add(v,u);
			add(u+n,v+n);
		}
		if(a==1&&b==1){
			add(u,v+n);
			add(v,u+n);
		}
	}
	for(int i=1;i<=2*n;i++){
		if(!dfn[i]){
			tj(i);
		}
	}
	for(int i=1;i<=n;i++){
		if(qiang[i]==qiang[i+n]){
			cout<<"IMPOSSIBLE";
			return 0;
		}
	}
	cout<<"POSSIBLE\n";
	for(int i=1;i<=n;i++){
		cout<<(qiang[i+n]<qiang[i])<<" ";
	}
	return 0;
}
2025/1/23 10:57
加载中...