HDU 4115 2-SAT

http://vjudge.net/contest/view.action?cid=48609#problem/A

Description

Conflicts are everywhere in the world, from the young to the elderly, from families to countries. Conflicts cause quarrels, fights or even wars. How wonderful the world will be if all conflicts can be eliminated.
Edward contributes his lifetime to invent a 'Conflict Resolution Terminal' and he has finally succeeded. This magic item has the ability to eliminate all the conflicts. It works like this:
If any two people have conflict, they should simply put their hands into the 'Conflict Resolution Terminal' (which is simply a plastic tube). Then they play 'Rock, Paper and Scissors' in it. After they have decided what they will play, the tube should be opened and no one will have the chance to change. Finally, the winner have the right to rule and the loser should obey it. Conflict Eliminated!
But the game is not that fair, because people may be following some patterns when they play, and if the pattern is founded by others, the others will win definitely.
Alice and Bob always have conflicts with each other so they use the 'Conflict Resolution Terminal' a lot. Sadly for Bob, Alice found his pattern and can predict how Bob plays precisely. She is very kind that doesn't want to take advantage of that. So she tells Bob about it and they come up with a new way of eliminate the conflict:
They will play the 'Rock, Paper and Scissors' for N round. Bob will set up some restricts on Alice.
But the restrict can only be in the form of "you must play the same (or different) on the ith and jth rounds". If Alice loses in any round or break any of the rules she loses, otherwise she wins.
Will Alice have a chance to win?
 

Input

The first line contains an integer T(1 <= T <= 50), indicating the number of test cases.
Each test case contains several lines.
The first line contains two integers N,M(1 <= N <= 10000, 1 <= M <= 10000), representing how many round they will play and how many restricts are there for Alice.
The next line contains N integers B 1,B 2, ...,B N, where B i represents what item Bob will play in the i th round. 1 represents Rock, 2 represents Paper, 3 represents Scissors.
The following M lines each contains three integers A,B,K(1 <= A,B <= N,K = 0 or 1) represent a restrict for Alice. If K equals 0, Alice must play the same on A th and B th round. If K equals 1, she must play different items on Ath and Bthround.
 

Output

For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is "yes" or "no" represents whether Alice has a chance to win.
 

Sample Input

     
     
     
     
2 3 3 1 1 1 1 2 1 1 3 1 2 3 1 5 5 1 2 3 2 1 1 2 1 1 3 1 1 4 1 1 5 1 2 3 0
 

Sample Output

     
     
     
     
Case #1: no Case #2: yes

Hint

'Rock, Paper and Scissors' is a game which played by two person. They should play Rock, Paper or Scissors by their hands at the same time. 
Rock defeats scissors, scissors defeats paper and paper defeats rock. If two people play the same item, the game is tied..
 
题目大意:

                两个人玩“石头剪刀布”的游戏,A知道B每次都出什么,为了尽量体现公平,给A提出了两个要求,输入x,y,z,若z为1那么要求第x次和第y次A不能出一样的,若Z为0则第x次和第y次必须出一样的。在这种前提下,如果A有一次输了,或者违反了这两个要求,就算A失败,否则A胜利。

简单思路:

                因为要求A一次也不能输,那么对于A来说,每一次必须与B平局或者胜利才行。所以A每次有两种选择。根据对他的要求判断第x次和第y次B出的是否一样,来确定对A的状态限制。

其实就是一个2-SAT问题,我们一般采用连通性来解决。若每一次的两种状态,在同一个强连通分量里面就不能满足2-SAT问题有解;

节点是从零开始的,2*i+1表示胜,2*i表示平,不能选负。

</pre><pre name="code" class="cpp">#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
const int N=10205;
int head[N*2],ip,dfn[N*2],low[N*2],instack[N*2],cor[N*2];
int n,m,scc,cnt,a[N*2],b[N*2][2];
struct note
{
    int v,next;
}edge[N*10];
stack<int>p;
void init()
{
    memset(head,-1,sizeof(head));
    ip=0;
}
void addedge(int u,int v)
{
    edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;
}
void tarjan(int u)
{
    dfn[u]=low[u]=++cnt;
    p.push(u);
    instack[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(low[u]==dfn[u])
    {
        ++scc;
        int temp;
        do
        {
            temp=p.top();
            p.pop();
            cor[temp]=scc;
            instack[temp]=0;
        }while(temp!=u);
    }
}
void solve()
{
    while(!p.empty())
        p.pop();
    cnt=scc=0;
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(cor,0,sizeof(cor));
    memset(instack,0,sizeof(instack));
    for(int i=0;i<n*2;i++)
    {
        if(!dfn[i])
            tarjan(i);
    }
}
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            b[i][0]=a[i];
            if(a[i]==1)  b[i][1]=2;
            if(a[i]==2)  b[i][1]=3;
            if(a[i]==3)  b[i][1]=1;
        }
        int x,y,z;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            x--,y--;
            if(z)
            {
                if(b[x][0]==b[y][0])
                   addedge(x*2,y*2+1),addedge(y*2,x*2+1);
                if(b[x][0]==b[y][1])
                    addedge(x*2,y*2),addedge(y*2+1,x*2+1);
                if(b[x][1]==b[y][0])
                    addedge(x*2+1,y*2+1),addedge(y*2,x*2);
                if(b[x][1]==b[y][1])
                    addedge(x*2+1,y*2),addedge(y*2+1,x*2);
            }
            else
            {
                if(b[x][0]!=b[y][0])
                    addedge(x*2,y*2+1),addedge(y*2,x*2+1);
                if(b[x][0]!=b[y][1])
                    addedge(x*2,y*2),addedge(y*2+1,x*2+1);
                if(b[x][1]!=b[y][0])
                    addedge(x*2+1,y*2+1),addedge(y*2,x*2);
                if(b[x][1]!=b[y][1])
                    addedge(x*2+1,y*2),addedge(y*2+1,x*2);
            }
        }
        solve();
        int flag=1;
        for(int i=0;i<n;i++)
        {
            if(cor[i*2]==cor[i*2+1])
            {
                flag=0;
                break;
            }
        }
        printf("Case #%d: ",++tt);
        if(flag)
            puts("yes");
        else
            puts("no");
    }
    return 0;
}

你可能感兴趣的:(HDU 4115 2-SAT)