并不对劲的2-SAT
说明
WAWAWAWA
建的边“不完整”,比如当限制是“x为1时y一定为1”时,连x->y的边时,忘记连y'->x'的边(逆否)。
代码
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<set>
#include<stack>
#include<vector>
#include<queue>
#define LL long long
#define maxn 2000007
#define rep(i,x,y) for(int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(int i=(x);i>=(y);--i)
#define view(u,k) for(int k=fir[u];~k;k=nxt[k])
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
void write(int x)
{
int f=0;char ch[20];
if(x==0){putchar('0');putchar(' ');return ;}
if(x<0){putchar('-'),x=-x;}
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);putchar(' ');
}
int n,m,fir[maxn],nxt[maxn],v[maxn],cnte,dfn[maxn],low[maxn],ans[maxn],ins[maxn],stk[maxn],tp,tim,col[maxn],num;
void ade(int u1,int v1){v[cnte]=v1,nxt[cnte]=fir[u1],fir[u1]=cnte++;}
void tar(int u)
{
dfn[u]=low[u]=++tim,ins[u]=1,stk[++tp]=u;
view(u,k)
{
if(!dfn[v[k]])tar(v[k]),low[u]=min(low[u],low[v[k]]);
else if(ins[v[k]])low[u]=min(low[u],dfn[v[k]]);
}
if(dfn[u]==low[u])
{
num++;
while(1)
{
col[stk[tp]]=num,ins[stk[tp]]=0;
if(stk[tp--]==u)break;
}
}
}
int gx(int x,int f){return x+f*n;}
int main()
{
memset(fir,-1,sizeof(fir));
n=read(),m=read();
rep(i,1,m)
{
int x1=read(),k1=read(),x2=read(),k2=read();
if(x1==x2&&k1!=k2)continue;
else if(x1==x2)ade(gx(x1,k1^1),gx(x1,k1));
else ade(gx(x1,k1^1),gx(x2,k2)),ade(gx(x2,k2^1),gx(x1,k1));
}int li=n<<1;
rep(i,1,li)if(!dfn[i])tar(i);
rep(i,1,n)
{
if(col[gx(i,0)]==col[gx(i,1)]){puts("IMPOSSIBLE");return 0;}
if(col[gx(i,0)]>col[gx(i,1)])ans[i]=1;
else ans[i]=0;
}
puts("POSSIBLE");
rep(i,1,n)write(ans[i]);
return 0;
}