2-sat, the complexity is O (M), each point is divided into 2 points, representing the election or not, and then build a principle, that is, a forward edge x->y representative x selected y is required, and then build the Tarjan, if there are 2 points in the same ring, no solution, otherwise set up anti-topological flutter sort, search to a point, He chose not to choose his opposite point and his successor.
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6#include <vector>7 inttot,go[200005],first[200005],next[200005];8 intinstack[200005],num,sz,low[200005],dfn[200005];9 intvis[200005],st[200005],top;Ten intc[200005],belong[200005],n,m,ru[200005],op[200005],col[200005]; Onestd::vector<int> son[200005]; A intRead () { - intt=0, f=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} the while('0'<=ch&&ch<='9') {t=t*Ten+ch-'0'; ch=GetChar ();} - returnt*F; - } - voidInsertintXinty) { +tot++; -go[tot]=y; +next[tot]=First[x]; Afirst[x]=tot; at } - voidTarjan (intx) { -instack[x]=1; low[x]=dfn[x]=++sz; -vis[x]=1; st[++top]=x; - for(intI=first[x];i;i=Next[i]) { - intPur=Go[i]; in if(!Vis[pur]) { - Tarjan (PUR); tolow[x]=std::min (Low[x],low[pur]); +}Else if(Instack[pur]) { -low[x]=std::min (Low[x],dfn[pur]); the } * } $ if(low[x]==Dfn[x]) {Panax Notoginsengnum++; - while(st[top]!=x) { theinstack[st[top]]=0; +belong[st[top]]=num; Atop--; the } +instack[x]=0; -belong[x]=num; $top--; $ } - } - voidDfsintx) { the if(Col[x])return; col[x]=-1; - for(intI=0; I<son[x].size (); i++){Wuyi DFS (Son[x][i]); the } - } Wu voidsolve () { - intt=0; About for(intI=1; i<=num;i++)if(!ru[i]) c[++t]=i; $ while(t) { - intnow=c[t--]; - if(Col[now])Continue; -col[now]=1; A DFS (Op[now]); + for(intI=0; I<son[now].size (); i++){ the if(! (--ru[son[now][i])) c[++t]=Son[now][i]; - } $ } the } the intMain () { theFreopen ("gates.in","R", stdin); theFreopen ("Gates.out","W", stdout); -N=read (); m=read (); in for(intI=1; i<=n;i++){ the intA=read (), Sa=read (), B=read (), sb=read (); the if(sa==0){ About if(sb==0) Insert (a<<1, (b<<1)-1), insert (b<<1, (a<<1)-1); the ElseInsert (a<<1,b<<1), insert ((b<<1)-1, (a<<1)-1); the}Else{ the if(sb==0) Insert ((a<<1)-1, (b<<1)-1), insert (b<<1,a<<1); + ElseInsert ((a<<1)-1,b<<1), insert ((b<<1)-1,a<<1); - } the }Bayi for(intI=1;i<=m<<1; i++)if(!Vis[i]) Tarjan (i); the for(intI=1; i<=m;i++) the if(Belong[(i<<1)-1]==belong[i<<1]){ -Puts"Impossible"); - return 0; the } the for(intI=1;i<=m<<1; i++) the for(intj=first[i];j;j=Next[j]) { the intPur=Go[j]; - if(Belong[pur]==belong[i])Continue; the Son[belong[pur]].push_back (Belong[i]); theru[belong[i]]++; the }94 for(intI=1; i<=m;i++) theop[belong[i<<1]]=belong[(i<<1)-1],op[belong[(i<<1)-1]]=belong[i<<1]; the solve (); the for(intI=1; i<=m;i++)if(col[belong[i<<1]]==1) puts ("1");ElsePuts"0"); 98}
Waste water Recycling