HDU 3715 go deeper (2-sat + binary)

Source: Internet
Author: User

[Question link]

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3715

[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 look at Row 4. If conditions are metDep <m and X [A [Dep] + X [B [Dep]! = C [Dep]Then we can go to the next layer for recursion. array X takes only {0, 1}, array c Takes {0, 1, 2}, and array A and array B takes 0 ~ M and M are the maximum number of recursive layers and the size of array X. How many layers can be recursion at most?

[Idea]

For each X [I], only 0 or 1 can be taken. If conditions are met in each layer, the next layer can be entered. This question is very similar to poj 2723.

You can create a 2-Sat graph for the two layers that can be recursive at most.

[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;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;}

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.