HDU 3715 Go Deeper (2-sat + two)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

"Topic link"

http://acm.hdu.edu.cn/showproblem.php?pid=3715


"The main topic"

There is a recursive code:


go (int dep, int n, int m)
     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 look at line fourth, if 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 only takes {0, 1}, the C array takes {0,1,2}, and A and B numbers  The group takes 0~m, M is the maximum number of recursive layers, and the size of the array x. How many layers can I recursively ask?



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 similar to POJ 2723, the same practice.

The number of layers can be recursive at most, and then the 2-sat of these layers is determined.


Code

#include <iostream> #include <queue> #include <stack> #include <cstdio> #include <cstring > #include <vector> #define MP make_pair#define SQ (x) ((x) * (x)) using namespace Std;const int INF = 0x3f3f3f3f;cons t 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{in    T 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;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.