HDU 3715 Go Deeper

來源:互聯網
上載者:User

標籤:

二分答案 + 2-SAT判斷

#include<cstdio>#include<cstring>#include<cmath>#include<vector>#include<algorithm>using namespace std;const int maxn=8000+5;int M,N,T;int ans;int L,R,Mid;int a[10010],b[10010],c[10010];struct TwoSAT{    int n;    vector<int> G[maxn*2];    bool mark[maxn*2];    int S[maxn*2],c;    bool dfs(int x)    {        if(mark[x^1]) return false;        if(mark[x]) return true;        mark[x]=true;        S[c++]=x;        for(int i=0;i<G[x].size();i++)            if(!dfs(G[x][i])) return false;        return true;    }    void init(int n)    {        this->n=n;        for(int i=0;i<n*2;i++) G[i].clear();        memset(mark,0,sizeof mark);    }    void add_clause(int x,int y)    {        G[x].push_back(y^1);        G[y].push_back(x^1);    }    bool solve()    {        for(int i=0;i<2*n;i+=2)            if(!mark[i]&&!mark[i+1])            {                c=0;                if(!dfs(i))                {                    while(c>0) mark[S[--c]]=false;                    if(!dfs(i+1)) return false;                }            }        return true;    }};int main(){    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&N,&M);        for(int i=0;i<M;i++)            scanf("%d%d%d",&a[i],&b[i],&c[i]);        L=0,R=M;              while(L<=R)        {            Mid=(L+R)/2;            TwoSAT T; T.init(N);            for(int i=0;i<Mid;i++)            {                if(c[i]==2)                {                    T.add_clause(2*a[i]+1,2*b[i]+1);                }                else if(c[i]==1)                {                    T.add_clause(2*a[i]+1,2*b[i]);                    T.add_clause(2*a[i],2*b[i]+1);                }                else if(c[i]==0)                {                    T.add_clause(2*a[i],2*b[i]);                }            }            if(T.solve())            {                ans=Mid;                L=Mid+1;            }            else R=Mid-1;        }        printf("%d\n",ans);    }    return 0;}

 

HDU 3715 Go Deeper

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.