hdu 4115 Eliminate the Conflict【2-SAT---------Tarjan强连通】

Eliminate the Conflict

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

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

 

题目大意:

一共进行n轮石头剪刀布的游戏,1代表剪刀,2代表石头,3代表布。

输入n,m,表示一共有n论游戏,m个限制条件,接下来一行包含n个数,其表示每一轮Bob出的是什么手势。

接下来m行限制条件,每一行包含三个元素a,b,c ,如果c==0,表示第a轮和第b轮Alice的出拳必须相同,如果c==1表示第a轮和第b轮的Alice的出拳必须不同。

如果Alice输了其中任一一轮或者是出拳矛盾,那么就输出no,否则输出yes、


思路:


1、因为我们知道Bob每一轮出的拳是什么,那么我们应对这一轮的出拳,一定不会去选择能够让Bob赢的那一种出拳,那么这个时候,对于每一轮Alice的出拳选择,就从三种变成了两种,这个时候我们就能巧妙的将问题转化到:一共有n轮(n个党派),每一轮两种出拳方式(每一个党派两个人),问是否能够找到一种出拳方式(每个党派出一个人,不矛盾的情况下),并且满足m个限制条件的前提下(m个党派矛盾关系),使得Alice满足的胜利的条件(一共找到n个人)。


2、建点:

那么我们不妨将Alice每一轮的出拳方式都设为一个点:

Bob(出拳方式)111
A剪刀147
A石头258
A布369
例如上表,节点1表示第一轮Alice出剪刀。
那么对于样例1,n个党派分成的情况为这样(因为Bob出的是剪刀,所以我们党派分布人员的时候,就不能选代表布的节点,那样Alice就输了):

党派1党派2党派3
147
258

3、 建边:

做过2-Sat题目的小伙伴们应该知道,对于2-sat的判定的时候,找到矛盾边(a,b),那么对应就要建立两条有向边(a,!b)(b,!a);

那么我们讨论c的值对应存在的矛盾边:

①if(c==0)要求的是必须两次出拳相同,那么我们就找两次出拳不同的情况,作为矛盾边(a,b)然后建立(a,!b)(b,!a)。

②if(c==1)要求的是必须两次出拳不同,那么我们就找两次出拳相同的情况,作为矛盾边(a,b)然后建立(a,!b)(b,!a)。

对于出拳是否相同,我们在建点的时候,对应编号如果a%3==b%3,那么这两个点就代表其那两轮的出拳相同。


4、代码实现,Tarjan强连通+邻接表+建点,,具体实现参考代码即可。


5、注意的点:注意输出格式。


AC代码;


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int head[1000000];
struct EdgeNode
{
    int to;
    int w;
    int next;
}e[1000000];
int b[100005];
int dang[100005][2];
int color[100005*3];
int vis[100005*3];
int low[100005*3];
int dfn[100005*3];
int stack[100005*4];
int n,m,cont,cnt,tt,sig;
void add(int from,int to)
{
    e[cont].to=to;
    e[cont].next=head[from];
    head[from]=cont++;
}
void Tarjan(int u)
{
    vis[u]=1;
    dfn[u]=low[u]=cnt++;
    stack[++tt]=u;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(vis[v]==0)Tarjan(v);
        if(vis[v]==1)low[u]=min(low[u],low[v]);
    }
    if(dfn[u]==low[u])
    {
        sig++;
        do
        {
            color[stack[tt]]=sig;
            vis[stack[tt]]=-1;
        }
        while(stack[tt--]!=u);
    }
}
int Slove()
{
    sig=0;
    tt=-1;
    sig=0;
    memset(color,0,sizeof(color));
    memset(vis,0,sizeof(vis));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(stack,0,sizeof(stack));
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<2;j++)
        {
            if(vis[dang[i][j]]==0)
            Tarjan(dang[i][j]);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(color[dang[i][0]]==color[dang[i][1]])return 0;
    }
    return 1;
}
int main()
{
    int t;
    int kase=0;
    scanf("%d",&t);
    while(t--)
    {
        cont=0;
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
            dang[i][0]=(i-1)*3+b[i];
            if(dang[i][0]%3==0)dang[i][1]=dang[i][0]-2;
            else dang[i][1]=dang[i][0]+1;
        }
        for(int i=0;i<m;i++)
        {
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            if(c==0)
            {
                for(int j=0;j<2;j++)
                {
                    for(int k=0;k<2;k++)
                    {
                        if(dang[x][j]%3==dang[y][k]%3)continue;
                        add(dang[x][j],dang[y][k^1]);
                        add(dang[y][k],dang[x][j^1]);
                    }
                }
            }
            if(c==1)
            {
                for(int j=0;j<2;j++)
                {
                    for(int k=0;k<2;k++)
                    {
                        if(dang[x][j]%3==dang[y][k]%3)
                        {
                            add(dang[x][j],dang[y][k^1]);
                            add(dang[y][k],dang[x][j^1]);
                        }
                    }
                }
            }
        }
        printf("Case #%d: ",++kase);
        int out=Slove();
        if(out==0)
        {
            printf("no\n");
        }
        else
        {
            printf("yes\n");
        }
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值