2-SAT

#include<bits/stdc++.h>
using namespace std;
#define N 2000005
int n,m,x,y,xx,yy,dfn[N],low[N],zhan[N],tot,top,h[N],col[N],u[N],k,cnt;
struct AB{
    int a,b,n;
}d[N];
void cun(int x,int y){d[++k]=(AB){x,y,h[x]},h[x]=k;}
void tarjan(int x){
    dfn[x]=low[x]=++tot,zhan[++top]=x,u[x]=1;
    for(int i=h[x];i;i=d[i].n){
        int y=d[i].b;
        if(!dfn[y]){
            tarjan(y);
            low[x]=min(low[x],low[y]);
        }
        else if(u[y]) low[x]=min(low[x],dfn[y]);
    }
    if(low[x]==dfn[x]){
        cnt++;
        while(zhan[top]!=x) col[zhan[top]]=cnt,u[zhan[top]]=0,top--;
        col[zhan[top]]=cnt,u[zhan[top]]=0,top--;
    }
}
void TARJAN(){
    for(int i=1;i<=n*2;i++){
        if(!dfn[i]) tarjan(i);
    }
}
int wujie(){
    for(int i=1;i<=n;i++){
        if(col[i]==col[i+n]) return 1;
    }
    return 0;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d%d",&x,&xx,&y,&yy);
        if(xx==0&&yy==0) cun(x+n,y),cun(y+n,x);
        if(xx==0&&yy==1) cun(x+n,y+n),cun(y,x);
        if(xx==1&&yy==0) cun(x,y),cun(y+n,x+n);
        if(xx==1&&yy==1) cun(x,y+n),cun(y,x+n);
    }
    TARJAN();
    if(wujie()){
        printf("IMPOSSIBLE\n");
        return 0;
    }
    printf("POSSIBLE\n");
    for(int i=1;i<=n;i++){
        if(col[i]>col[i+n]) printf("1 ");
        else printf("0 ");
    }
    printf("\n");
    return 0;
}
posted @ 2022-08-11 20:02  hubingshan  阅读(10)  评论(0)    收藏  举报  来源