hdu 4115 Eliminate the Conflict (2-sat)

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 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..
 

Source

2011 Asia ChengDu Regional Contest



题意:

Bob和Alice玩剪刀石头布,一个玩n轮,Alice已经知道了Bob每次要出什么,1代表剪刀,2代表石头,3代表布,然后Bob对Alice作出了一些限制:

给m行,每行是a b k,如果k是0,表示Alice第a次和b次出的拳必须相同,如果k是1,表示Alice第a次和b次出的拳必须不相同。

一但Alice破坏了这个限制规则,或者输了一局,那么Alice就彻底输了。

问Alice可不可能赢?


思路:每一次出拳Alice只能选择赢或则平两种选择,所以2-sat算法来做;

i 表示赢,i+n表示平,根据限制,我们可以建立关系图,然后2-sat判断是否矛盾


代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=20005;
const int M=50005;
struct node{
    int now,next;
}str[M];
int map[N];
int tot,head[N],vis[N],top,s[N];
void add(int x,int y)
{
    str[tot].now=y;
    str[tot].next=head[x];
    head[x]=tot++;
}
bool dfs(int u,int n)
{
    if(vis[u+n]) return false;
    if(vis[u]) return true;
    s[top++]=u;
    vis[u]=1;
    for(int i=head[u];i!=-1;i=str[i].next)
    {
        int v=str[i].now;
        if(!dfs(v,n))
            return false;
    }
    return true;
}
bool ok(int n)
{
    memset(vis,0,sizeof(vis));
    for(int i=0;i<n;i++)
    {
        if(!vis[i]&&!vis[i+n])
        {
            top=0;
            if(!dfs(i,n))
            {
                while(top>0)
                    vis[s[--top]]=0;
                if(!dfs(i+n,n))
                    return false;
            }
        }
    }
    return true;
}
int main()
{
    int t,T=1;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        memset(head,-1,sizeof(head));
        tot=0;
        for(int i=0;i<n;i++)
            scanf("%d",&map[i]);
        for(int i=0;i<m;i++)
        {
            int a,b,k;
            scanf("%d%d%d",&a,&b,&k);
            a--;b--;
            if(k==0)
            {
                if(map[a]==map[b])
                {
                    add(a,b);  //a表示赢,k=0,出拳一样则b也必定赢
                    add(b,a);
                    add(b+n,a+n);
                    add(a+n,b+n);
                }
                else
                {
                    if(map[a]==1&&map[b]==2)
                    {
                        add(a,b+n);
                        add(b+n,a);
                        add(a+n,a);
                        add(b,b+n);
                    }
                    else if(map[a]==1&&map[b]==3)
                    {
                        add(a,a+n);
                        add(b,a+n);
                        add(a+n,b);
                        add(b+n,b);
                    }
                    else if(map[a]==2&&map[b]==3)
                    {
                        add(a,b+n);
                        add(a+n,a);
                        add(b,b+n);
                        add(b+n,a);
                    }
                    else if(map[a]==2&&map[b]==1)
                    {
                        add(b,a+n);
                        add(a+n,b);
                        add(b+n,b);
                        add(a,a+n);
                    }
                    else if(map[a]==3&&map[b]==1)
                    {
                        add(b,b+n);
                        add(a,b+n);
                        add(b+n,a);
                        add(a+n,a);
                    }
                    else if(map[a]==3&&map[b]==2)
                    {
                        add(b,a+n);
                        add(b+n,b);
                        add(a,a+n);
                        add(a+n,b);
                    }
                }
            }
            else
            {
                if(map[a]==map[b])
                {
                    add(a,b+n);
                    add(b,a+n);
                    add(b+n,a);
                    add(a+n,b);
                }
                else
                {
                    if(map[a]==1&&map[b]==2)
                    {
                        add(a,b);
                        add(b+n,a+n);
                    }
                    else if(map[a]==1&&map[b]==3)
                    {
                        add(b,a);
                        add(a+n,b+n);
                    }
                    else if(map[a]==2&&map[b]==3)
                    {
                        add(a,b);
                        add(b+n,a+n);
                    }
                    else if(map[a]==2&&map[b]==1)
                    {
                        add(b,a);
                        add(a+n,b+n);
                    }
                    else if(map[a]==3&&map[b]==1)
                    {
                        add(a,b);
                        add(b+n,a+n);
                    }
                    else if(map[a]==3&&map[b]==2)
                    {
                        add(b,a);
                        add(a+n,b+n);
                    }
                }
            }
        }
        printf("Case #%d: ",T++);
        if(ok(n))
            printf("yes\n");
        else
            printf("no\n");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值