hdu 3715 Go Deeper(二分+2-sat判定)

來源:互聯網
上載者:User
Go Deeper

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 988    Accepted Submission(s): 351


Problem DescriptionHere is a procedure's pseudocode:

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

In this code n is an integer. a, b, c and x are 4 arrays of integers. The index of array always starts from 0. Array a and b consist of non-negative integers smaller than n. Array x consists of only 0 and 1. Array c consists of only 0, 1 and 2. The lengths
of array a, b and c are m while the length of array x is n. Given the elements of array a, b, and c, when we call the procedure go(0, n, m) what is the maximal possible value the procedure may output?

 


InputThere are multiple test cases. The first line of input is an integer T (0 < T ≤ 100), indicating the number of test cases. Then T test cases follow. Each case starts with a line of 2 integers n and m (0 < n ≤ 200, 0 < m ≤ 10000). Then m lines of 3 integers
follow. The i-th(1 ≤ i ≤ m) line of them are ai-1 ,bi-1 and ci-1 (0 ≤ ai-1, bi-1 < n, 0 ≤ ci-1 ≤ 2). 


OutputFor each test case, output the result in a single line. 


Sample Input

32 10 1 02 10 0 02 20 1 01 1 2
 


Sample Output

112
 


AuthorCAO, Peng 


Source2010 Asia Chengdu Regional Contest 


Recommendzhouzeyong 題目:http://acm.hdu.edu.cn/showproblem.php?pid=3715題意:給你m組數a、b、c,求滿足 x[a]+x[b]!=c的最多連續的組數,x為長度n 的數組,值未確定,為0或1分析:我們可以假設x數組的所有值,然後依次判定有幾個式子成立,然後就想到了2-sat。。。也就反過來想了,假設前i個式子成立,那麼可以用2-sat判斷,是否能滿足前i個數組成立,由於m比較大,加個二分就ok代碼:

#include<cstdio>#include<iostream>using namespace std;const int mm=44444;const int mn=444;int ver[mm],next[mm],a[mm],b[mm],c[mm];int head[mn],dfn[mn],low[mn],q[mn],id[mn];int i,j,k,n,m,l,r,t,mid,idx,top,cnt,edge,ans;void add(int u,int v){    ver[edge]=v,next[edge]=head[u],head[u]=edge++;}void dfs(int u){    dfn[u]=low[u]=++idx;    q[top++]=u;    for(int i=head[u],v;i>=0;i=next[i])        if(!dfn[v=ver[i]])            dfs(v),low[u]=min(low[u],low[v]);        else if(!id[v])low[u]=min(low[u],dfn[v]);    if(dfn[u]==low[u])    {        id[u]=++cnt;        while(q[--top]!=u)id[q[top]]=cnt;    }}void Tarjan(){    for(idx=cnt=top=i=0;i<n+n;++i)dfn[i]=id[i]=0;    for(i=0;i<n+n;++i)        if(!dfn[i])dfs(i);}bool ok(){    Tarjan();    for(i=0;i<n+n;i+=2)        if(id[i]==id[i^1])return 0;    return 1;}int main(){    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(i=0;i<m;++i)            scanf("%d%d%d",&a[i],&b[i],&c[i]);        ans=l=0,r=m;        while(l<=r)        {            mid=(l+r)>>1;            for(edge=i=0;i<n+n;++i)head[i]=-1;            for(i=0;i<mid;++i)            {                if(c[i]==0)add(a[i]<<1,b[i]<<1|1),add(b[i]<<1,a[i]<<1|1);                if(c[i]==1)add(a[i]<<1,b[i]<<1),add(b[i]<<1,a[i]<<1),                           add(a[i]<<1|1,b[i]<<1|1),add(b[i]<<1|1,a[i]<<1|1);                if(c[i]==2)add(a[i]<<1|1,b[i]<<1),add(b[i]<<1|1,a[i]<<1);            }            if(ok())ans=mid,l=mid+1;            else r=mid-1;        }        printf("%d\n",ans);    }    return 0;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.