首页 > 代码库 > hdu 4115 Eliminate the Conflict

hdu 4115 Eliminate the Conflict

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 B1,B2, ...,BN, where Bi represents what item Bob will play in the ith 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 Ath and Bth 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..
这个题有意思了啊,看着感觉是3-SAT,可是分析一下是2-SAT,简单,所谓的2-SAT不就是建立一个有向图,再求SCC,以就是强连通分量,再看看同一个点的两种状态在不在同一个SCC上,简单题,
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define  inf 0x0f0f0f0f
using namespace std;
int finger[10000+10][2];
struct SCC
{
    static const int maxn=20000+10;
    vector<int>group[maxn],scc[maxn];
    int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt,n,m;
    stack<int>S;

    void init()
    {
        for (int i=0;i<=n;i++) group[i].clear();
    }

    void addedge(int from,int to)
    {
        group[from].push_back(to);
    }

    void dfs(int u)
    {
        pre[u]=lowlink[u]=++dfs_clock;
        S.push(u);
        for (int i=0;i<group[u].size();i++)
        {
            int v=group[u][i];
            if (!pre[v])
            {
                dfs(v);
                lowlink[u]=min(lowlink[u],lowlink[v]);
            }
            else if (!sccno[v])
            {
                lowlink[u]=min(lowlink[u],pre[v]);
            }
        }
        if (lowlink[u]==pre[u])
        {
            scc_cnt++;
            scc[scc_cnt].clear();
            while (1)
            {
                int x=S.top();
                S.pop();
                scc[scc_cnt].push_back(x);
                sccno[x]=scc_cnt;
                if (x==u) break;
            }
        }
    }

    void find_scc()
    {
        dfs_clock=scc_cnt=0;
        memset(pre,0,sizeof(pre));
        memset(sccno,0,sizeof(sccno));
        for (int i=1;i<=n;i++)
        if (!pre[i]) dfs(i);
    }
};

SCC Conflict;

int main()
{
    int T,N,M,x,y,c,cas=0;
    scanf("%d",&T);
    while (T--)
    {
        cas++;
        scanf("%d%d",&N,&M);
        Conflict.n=N*2;
        Conflict.init();
        for (int i=0;i<N;i++)
        {
            scanf("%d",&x);
            if (x==1)
            {
                finger[i][0]=1;
                finger[i][1]=2;
            }
            if (x==2)
            {
                finger[i][0]=2;
                finger[i][1]=3;
            }
            if (x==3)
            {
                finger[i][0]=3;
                finger[i][1]=1;
            }
        }
        for (int i=1;i<=M;i++)
        {
            scanf("%d%d%d",&x,&y,&c);
            x--;y--;
            if (c==0)
            {
                for (int j=0;j<2;j++)
                {
                    for (int k=0;k<2;k++)
                    if (finger[x][j]!=finger[y][k])
                    {
                        Conflict.addedge(x*2+j,(y*2+k)^1);
                        Conflict.addedge(y*2+k,(x*2+j)^1);
                    }
                }
            }
            if (c==1)
            {
                for (int j=0;j<2;j++)
                {
                    for (int k=0;k<2;k++)
                    if (finger[x][j]==finger[y][k])
                    {
                        Conflict.addedge(x*2+j,(y*2+k)^1);
                        Conflict.addedge(y*2+k,(x*2+j)^1);
                    }
                }
            }
        }
        Conflict.find_scc();
        bool ans=true;
        for (int i=0;i<N;i+=2)
        {
            if (Conflict.sccno[i]==Conflict.sccno[i+1])
            {
                ans=false;
                break;
            }
        }
        if (ans) printf("Case #%d: yes\n",cas);
        else printf("Case #%d: no\n",cas);
    }
    return 0;
}

作者 chensunrise