This is a creation in Article, where the information may have evolved or changed.
Test instructions
There is such a process:
Go (int dep, int n, int m)
Begin
Output the value of DEP.
If DEP < M and X[A[DEP]] + X[B[DEP]]! = C[DEP] Then go (dep + 1, n, m)
End
Where x[] has a value of 0 or 1...c[] with a value of 0 or 1 or 2 .... Now tell a[],b[],c[]. What is the deepest possible question of this process?
Exercises
Look at the process. In fact, there is no way to go down on the M-layer. The deeper layers are definitely not going to make it. So satisfying monotonicity ... First read in a[],b[],c[] ... Then two minutes m ... Composition.. 2-sat. Tarjan judgment ....
Program:
#include <iostream> #include <stdio.h> #include <cmath> #include <queue> #include <stack> #include <string.h> #include <map> #include <set> #include <algorithm> #define OO 1000000007# Define MAXN 50005<<1#define Maxm 500005<<1#define ll long longusing namespace std; struct node{int y,next;} Line[maxm];int A[maxn],b[maxn],c[maxn],lnum,_next[maxn],dfn[maxn],low[maxn],tp[maxn],tpnum,dfsindex; BOOL instack[maxn];stack<int> mystack;void addline (int x,int y) {line[++lnum].next=_next[x],_next[x]=lnum,line [Lnum].y=y;} void Tarjan (int x) {Mystack.push (x), instack[x]=true; Dfn[x]=low[x]=++dfsindex; for (int k=_next[x];k;k=line[k].next) {int y=line[k].y; if (!dfn[y]) {Tarjan (y); Low[x]=min (Low[x],low[y]); }else if (Instack[y]) low[x]=min (Low[x],dfn[y]); } if (Low[x]==dFn[x]) {tpnum++; do {x=mystack.top (); Mystack.pop (); Tp[x]=tpnum; Instack[x]=false; }while (Low[x]!=dfn[x]); }}bool _2sat (int n,int M) {int i; lnum=0; memset (_next,0,sizeof (_next)); for (i=1;i<=m;i++) {int a=a[i],b=b[i],c=c[i]; if (!c) AddLine (a<<1,b<<1|1), AddLine (b<<1,a<<1|1); else if (c==1) {addline (a<<1,b<<1), AddLine (a<<1|1,b< ; <1|1); AddLine (b<<1,a<<1), AddLine (b<<1|1,a<<1|1); } else if (c==2) AddLine (a<<1|1,b<<1), AddLine (b<<1|1,a<<1); } while (!mystack.empty ()) Mystack.pop (); memset (dfn,0,sizeof (DFN)); memset (Instack,false,sizeof (instack)); dfsindex=tpnum=0; For (i=0;i< (n<<1); i++) if (!dfn[i]) Tarjan (i); for (i=0;i<n;i++) if (Tp[i<<1]==tp[i<<1|1]) return false; return true;} int main () {int n,m,cases; scanf ("%d", &cases); while (cases--) {scanf ("%d%d", &n,&m); for (int i=1;i<=m;i++) scanf ("%d%d%d", &a[i],&b[i],&c[i]); int l=0,r=m+1,mid; while (r-l>1) {mid=l+r>>1; if (_2sat (n,mid)) L=mid; else R=mid; } printf ("%d\n", L); } return 0;}