"The main effect of the topic"
There is a recursive 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
The key is to see line fourth, if the conditional DEP < M and X[a [DEP]] + X[B[DEP]]!= C[DEP] Then you can go to the next level of recursion, the x array takes {0, 1}, the C array takes {0,1,2}, and A and B arrays take 0~m, M is the maximum The number of recursive layers, also the size of the array x. How many layers can be recursively asked?
Ideas
For each X "I", can only take 0 or 1, in each layer if the conditions can be entered into the next layer, the problem is very much like POJ 2723, the practice is the same.
The maximum number of layers can be recursive, and then the 2-sat of these layers to build a map, can be judged.
Code
#include <iostream> #include <queue> #include <stack> #include <cstdio> #include <CSTRING&G
T
#include <vector> #define MP make_pair #define SQ (x) ((x) * (x)) using namespace Std;
const int INF = 0X3F3F3F3F;
const int MAXN = 10010;
const int VN = MAXN*2;
const int EN = VN*4;
int N, m, s;
int A[MAXN], B[MAXN], C[MAXN];
struct graph{int size, HEAD[VN]; Struct{int V, next;
E[en];
void Init () {size=0; memset (Head,-1, sizeof (head));
void Addedge (int u, int v) {e[size].v = v;
E[size].next = Head[u];
Head[u] = size++;
}}g;
Class two_sat{Public:bool Check (const graph& g, const int n) {SCC (g, 2*n);
for (int i=0; i<n; ++i) if (belong[i] = = Belong[i+n]) return false;
return true;
} private:void Tarjan (const graph& g, const int u) {int V; Dfn[u] = low[u] = ++idx;
sta[top++] = u;
Instack[u] = true;
for (int e=g.head[u]; E!=-1 e=g.e[e].next) {v = g.e[e].v;
if (dfn[v] = =-1) {Tarjan (g, v);
Low[u] = min (Low[u], low[v]);
}else if (Instack[v]) {Low[u] = min (Low[u], dfn[v]);
} if (dfn[u] = = Low[u]) {++bcnt;
do{v = sta[--top];
INSTACK[V] = false;
BELONG[V] = bcnt;
}while (U!= v);
} void SCC (const graph& g, const int n) {idx = top = bcnt = 0;
memset (DFN,-1, sizeof (DFN));
memset (instack, 0, sizeof (instack));
for (int i=0; i<n; ++i) {if (dfn[i] = = 1) Tarjan (g, I);
} private:int idx, top, bcnt; int DFN[VN], LOW[VN], STA[VN], BELONG[VN];
BOOL INSTACK[VN];
}sat;
void buildgraph (int dep) {g.init ();
for (int i=0; i<dep; ++i) {int x=a[i], y=b[i];
if (c[i]==0) {G.addedge (x, y+m);
G.addedge (y, x+m);
}else if (c[i] = = 1) {G.addedge (x, y);
G.addedge (X+m, y+m);
G.addedge (y, x);
G.addedge (Y+m, x+m);
}else if (c[i] = = 2) {G.addedge (x+m, y);
G.addedge (y+m, x);
int main () {int ncase;
scanf ("%d", &ncase);
while (ncase--) {scanf ("%d%d", &n, &m);
for (int i=0; i<m; ++i) {scanf ("%d%d%d", &a[i], &b[i], &c[i]);
int l=0, r=m+1, Mid, ans;
while (L < r) {mid = (L + r) >> 1;
Buildgraph (mid); if (Sat.check (g, m)) {L = mid+1;
Ans = mid;
else R = Mid;
printf ("%d\n", ans);
return 0; }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/