2-sat(石頭、剪刀、布)hdu4115

Eliminate the Conflict

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1732    Accepted Submission(s): 751

Problem 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..
題意:兩個人玩石頭剪刀布,進行n輪比賽,首先給出B的這n輪的手勢,然後m行對A的限制,每行a,b,c當c==0的時候代表A第a輪和第b輪的手勢相同,c==1表示第a輪和第b輪的手勢不同,如果A在任何一輪中輸掉或者違反規定的話,A就是輸的,問A有沒有贏的可能;
分析:開始看的時候每輪A都是有3個選擇,但是其實只有兩個合法的選擇,即平局和勝利的手勢,所以應該是用2-sat判矛盾,主要是建圖,當c==0時說明ab兩輪的手勢相同就是合法的,不同就是矛盾,對於矛盾建邊,c==1的時候同理;
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
#include"algorithm"
#include"string.h"
#include"string"
#include"stack"
#include"map"
#define inf 0x3f3f3f3f
#define M 20009
using namespace std;
struct node
{
    int u,v,next;
}edge[M*20];
stack<int>q;
int t,head[M],low[M],dfn[M],belong[M],num,index,use[M];
void init()
{
    t=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
    edge[t].u=u;
    edge[t].v=v;
    edge[t].next=head[u];
    head[u]=t++;
}
void tarjan(int u)
{
    low[u]=dfn[u]=++index;
    q.push(u);
    use[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(use[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        num++;
        int vv;
        do
        {
            vv=q.top();
            q.pop();
            use[vv]=0;
            belong[vv]=num;
        }while(vv!=u);
    }
}
int psq(int n)
{
    int i;
    num=index=n;
    memset(use,0,sizeof(use));
    memset(dfn,0,sizeof(dfn));
    for(i=1;i<=2*n;i++)
        if(!dfn[i])
        tarjan(i);
    for(i=1;i<=n;i++)
        if(belong[i]==belong[i+n])
        return 0;
    return 1;
}
int a[M],b[M];
int main()
{
    int T,m,n,i,u,v,w,kk=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
            a[i]=((b[i]+1)%3==0)?3:(b[i]+1)%3;
            a[i+n]=b[i];
        }
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            if(w==0)
            {
                if(a[u]!=a[v])
                {
                    add(u,v+n);
                    add(v,u+n);
                }
                if(a[u]!=a[v+n])
                {
                    add(u,v);
                    add(v+n,u+n);
                }
                if(a[u+n]!=a[v])
                {
                    add(u+n,v+n);
                    add(v,u);
                }
                if(a[u+n]!=a[v+n])
                {
                    add(u+n,v);
                    add(v+n,u);
                }
            }
            else
            {
                if(a[u]==a[v])
                {
                    add(u,v+n);
                    add(v,u+n);
                }
                if(a[u]==a[v+n])
                {
                    add(u,v);
                    add(v+n,u+n);
                }
                if(a[u+n]==a[v])
                {
                    add(u+n,v+n);
                    add(v,u);
                }
                if(a[u+n]==a[v+n])
                {
                    add(u+n,v);
                    add(v+n,u);
                }
            }
        }
        printf("Case #%d: ",kk++);
        if(psq(n))
            printf("yes\n");
        else
            printf("no\n");
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章