2-SAT

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e6+10;
int n,m;
int num;
int h[maxn],nxt[maxn],to[maxn],tot;
int dfn[maxn],low[maxn];
int zhan[maxn],top;
bool in_zhan[maxn];
int cnt;
int ans;
int a[maxn];
void add(int x,int y)
{
	tot++;
	to[tot]=y;
	nxt[tot]=h[x];
	h[x]=tot;
}
void tarjan(int x)
{
	dfn[x]=low[x]=++num;
	zhan[++top]=x;
	in_zhan[x]=true;
	for (int i=h[x];i;i=nxt[i])
	{
		int y=to[i];
		if (!dfn[y])
		{
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}
		else if (in_zhan[y])
		{
			low[x]=min(low[x],dfn[y]);
		}
	}
	if (dfn[x] == low[x])
	{
		cnt++;
		int y;
		do
		{
			y=zhan[top--];
			in_zhan[y] = false;
			a[y] = cnt;
		}while (y!=x);
	}
}
int main()
{
	cin >> n >> m;
	for (int i=1;i<=m;i++)
	{
		int a,b,x,y;
		cin >> a >> x >> b >> y;
		int nota=x^1;
		int notb=y^1;
		add(a+nota*n,b+y*n);
		add(b+notb*n,a+x*n);
	}
	for(int i=1;i<=n*2;i++)
	{
		if (!dfn[i]) tarjan(i);
	}
	for (int i=1;i<=n;i++)
	{
		if (a[i] == a[i+n])
		{
			cout << "IMPOSSIBLE" << endl;
			return 0;
		}
	}
	cout << "POSSIBLE" << endl;
	for (int i=1; i<=n;i++)
	{
		if (a[i]>a[i+n]) cout << "1" << " ";
		else cout << "0" <<  " ";
	}
	return 0;
}
posted @ 2026-03-24 11:47  msjing  阅读(0)  评论(0)    收藏  举报