Algorithm: Hdu 3715 go Deeper (2-sat + two points)

Source: Internet
Author: User
Tags bool int size min

"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 &lt;iostream&gt; #include &lt;queue&gt; #include &lt;stack&gt; #include &lt;cstdio&gt; #include &LT;CSTRING&G  
T  
    
    
#include &lt;vector&gt; #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&amp; g, const int n) {SCC (g, 2*n);  
        for (int i=0; i&lt;n; ++i) if (belong[i] = = Belong[i+n]) return false;  
    return true;  
   } private:void Tarjan (const graph&amp; 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&amp; g, const int n) {idx = top = bcnt = 0;  
        memset (DFN,-1, sizeof (DFN));  
        memset (instack, 0, sizeof (instack));  
        for (int i=0; i&lt;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&lt;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", &amp;ncase);  
            
        while (ncase--) {scanf ("%d%d", &amp;n, &amp;m);  
        for (int i=0; i&lt;m; ++i) {scanf ("%d%d%d", &amp;a[i], &amp;b[i], &amp;c[i]);  
        int l=0, r=m+1, Mid, ans;  
            while (L &lt; r) {mid = (L + r) &gt;&gt; 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/

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.