G-Go deeper
Time limit:3000 Ms
Memory limit:0 KB
64bit Io format:% LLD & % LlU
Description
Here is a procedure's pseudo code:
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
In this CodeNIs an integer.A,B,CAndXAre 4 arrays of integers. The index of array always starts from 0. ArrayAAndBConsist of non-negative integers smallerN. ArrayXConsists of only 0 and 1. ArrayCConsists of only 0, 1 and 2. The lengths of ArrayA,BAndCAreMWhile the length of ArrayXIsN.
Given the elements of ArrayA,B, AndC, When we call the procedure go (0,N,M) What is the maximal possible value does the procedure output?
Input
There are multiple test cases. The first line of input is an integerT(0 <T≤ 100), indicating the number of test cases. ThenTTest Cases Follow. Each case starts with a line of 2 integersNAndM(0 <N≤ 200, 0 <m ≤ 10000). Then M lines of 3 integers follow.I-Th (1 ≤I≤M) Line of them areAi-1,Bi-1AndCi-1(0 ≤Ai-1,Bi-1<N, 0 ≤Ci-1≤ 2 ).
Output
For each test case, output the result in a single line.
Sample Input
32 10 1 02 10 0 02 20 1 01 1 2
Sample output
112
Question:
Given some equations, the array X [] is unknown. How many equations can be obtained at most X [a] + X [B]? = C can be satisfied.
C = 0, 1, 2
X [] = {0, 1}
This is equivalent to a bare 2sat problem.
It is strongly recommended to read the summary of kuangbin 2-Sat: http://www.cnblogs.com/kuangbin/archive/2012/10/05/2712429.html
In general, when A or B is connected to the edge of a'-> B and B '-> A, then the force connection is performed to determine whether a and A' appear '... in the same connected component, it is impossible if it is in.
Create two states of number A, that is, a and A', equivalent to X [a] = 1 and X [a'] = 0
X [a] + X [B]! = 0=>A or B => a'-> B and A-> B'
X [a] + X [B]! = 1 => (A and B) or (a' and B ') = A or B 'and A' or B => a'-> B' and B-> A and A-> B and B '->
X [a] + X [B]! = 2 => A' or B '=> A-> B' and a'-> B
Just follow the above steps to create a graph and determine
#include<cstdio>#include<cstring>int e[50000],pd[50000],be[800],ne[50000],all;int dfn[800],low[800],instack[800],belong[800],stack[800],stak,curr,num;int a,b,c,n,m,l,r,mid,flag;void add(int x,int y,int p){ e[++all]=y; pd[all]=p; ne[all]=be[x]; be[x]=all;}void tarjan(int x){ instack[x]=1; stack[++stak]=x; dfn[x]=low[x]=++curr; for(int j=be[x];j!=0;j=ne[j]) if(pd[j]<=mid){ if(!dfn[e[j]]){ tarjan(e[j]); if(low[x]>low[e[j]]) low[x]=low[e[j]]; }else if(instack[e[j]]&&low[x]>low[e[j]]) low[x]=low[e[j]]; } if(dfn[x]==low[x]){ int j; ++num; do{ j=stack[stak--]; instack[j]=0; belong[j]=num; }while(j!=x); }}int solve(){ curr=stak=num=0; memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(instack,0,sizeof(instack)); for(int i=0;i<2*n;i++) if(!dfn[i]) tarjan(i); flag=0; for(int i=0;i<n;i++) if(belong[2*i]==belong[2*i+1]){ flag=1; break; } return flag;}int main(){ int tt; scanf("%d",&tt); while(tt--){ scanf("%d%d",&n,&m); all=0; memset(e,0,sizeof(e)); memset(be,0,sizeof(be)); memset(ne,0,sizeof(ne)); memset(pd,0,sizeof(pd)); for(int i=0;i<m;i++){ scanf("%d%d%d",&a,&b,&c); switch (c){ case 0: add(2*a+1,2*b,i); add(2*b+1,2*a,i); break; case 1: add(2*a,2*b,i); add(2*b+1,2*a+1,i); add(2*b,2*a,i); add(2*a+1,2*b+1,i); break; case 2: add(2*a,2*b+1,i); add(2*b,2*a+1,i); break; } } l=0;r=m-1; while(l<r-1){ mid=(l+r)/2; if(solve()) r=mid; else l=mid; } mid=r; if(!solve()) printf("%d\n",r+1); else printf("%d\n",l+1); } return 0;}
View code