HDU 4115 Eliminate the Conflict (2-SAT)

Eliminate the Conflict

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


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

 

Source
 
 

题意:

有两个人玩一个石头剪刀布的游戏,两个人连续玩N轮,给出其中

一个人的N轮出的情况和该人对另外一个人的一些限制条件,有两种限制:

每种限制表示为:(a,b,c) ,如果c==0 则表示该人对另外一个人的限制为第a

局和第b局出的应该一样,如果c==1表示不一样,问另外一个人是否有赢的

可能。

 

2-SAT的题目一看就看出来了。两种选择,有的情况是矛盾的。

主要是建图,建图过程要细心,很容易出错的。

 

/*
HDU 4115
题意:

有两个人玩一个石头剪刀布的游戏,两个人连续玩N轮,给出其中
一个人的N轮出的情况和该人对另外一个人的一些限制条件,有两种限制:
每种限制表示为:(a,b,c) ,如果c==0 则表示该人对另外一个人的限制为第a
局和第b局出的应该一样,如果c==1表示不一样,问另外一个人是否有赢的
可能。


*/
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;

const int MAXN=22000;//

bool visit[MAXN];
queue<int>q1,q2;
//vector建图方法很妙
vector<vector<int> >adj; //原图    //中间一定要加空格把两个'>'隔开
vector<vector<int> >radj;//逆图
vector<vector<int> >dag;//缩点后的逆向DAG图
int n,m,cnt;

int id[MAXN],order[MAXN],ind[MAXN];//强连通分量,访问顺序,入度

void dfs(int u)
{
    visit[u]=true;
    int i,len=adj[u].size();
    for(i=0;i<len;i++)
      if(!visit[adj[u][i]])
        dfs(adj[u][i]);
    order[cnt++]=u;
}
void rdfs(int u)
{
    visit[u]=true;
    id[u]=cnt;
    int i,len=radj[u].size();
    for(i=0;i<len;i++)
      if(!visit[radj[u][i]])
        rdfs(radj[u][i]);
}
void korasaju()
{
    int i;
    memset(visit,false,sizeof(visit));
    for(cnt=0,i=0;i<2*n;i++)
      if(!visit[i])
        dfs(i);
    memset(id,0,sizeof(id));
    memset(visit,false,sizeof(visit));
    for(cnt=0,i=2*n-1;i>=0;i--)
      if(!visit[order[i]])
      {
          cnt++;//这个一定要放前面来
          rdfs(order[i]);
      }
}
bool solvable()
{
    for(int i=0;i<n;i++)
      if(id[2*i]==id[2*i+1])
        return false;
   return true;
}

void add(int x,int y)
{
    adj[x].push_back(y);
    radj[y].push_back(x);
}

int a[MAXN],b[MAXN];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    int x,y,z;
    scanf("%d",&T);
    int iCase=0;
    while(T--)
    {
        iCase++;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]==1)b[i]=2;
            else if(a[i]==2)b[i]=3;
            else b[i]=1;
            if(a[i]>b[i])swap(a[i],b[i]);
        }
        adj.assign(2*n,vector<int>());
        radj.assign(2*n,vector<int>());
        bool flag=true;
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            x--;
            y--;
            if(!flag)continue;
            if(x==y)
            {
                if(z==1)flag=false;
                continue;
            }
            if(z==0)//要相等
            {
                /*  //这种加边方法也可以
                if(a[x]==a[y]&&b[x]==b[y])
                {
                    adj[2*x].push_back(2*y);
                    adj[2*y].push_back(2*x);
                    adj[2*x+1].push_back(2*y+1);
                    adj[2*y+1].push_back(2*x+1);

                    radj[2*x].push_back(2*y);
                    radj[2*y].push_back(2*x);
                    radj[2*x+1].push_back(2*y+1);
                    radj[2*y+1].push_back(2*x+1);
                }
                else if(a[x]==a[y]&&b[x]!=b[y])//只能选a
                {
                    adj[2*x+1].push_back(2*x);
                    adj[2*y+1].push_back(2*y);
                    radj[2*x].push_back(2*x+1);
                    radj[2*y].push_back(2*y+1);
                }
                else if(a[x]!=a[y]&&b[x]==b[y])//只能选b
                {
                    adj[2*x].push_back(2*x+1);
                    adj[2*y].push_back(2*y+1);
                    radj[2*x+1].push_back(2*x);
                    radj[2*y+1].push_back(2*y);
                }
                else if(a[x]==b[y])//23和12
                {
                    adj[2*x+1].push_back(2*x);
                    adj[2*y].push_back(2*y+1);
                    radj[2*x].push_back(2*x+1);
                    radj[2*y+1].push_back(2*y);
                }
                else if(b[x]==a[y])
                {
                    adj[2*x].push_back(2*x+1);
                    adj[2*y+1].push_back(2*y);
                    radj[2*x+1].push_back(2*x);
                    radj[2*y].push_back(2*y+1);
                }
                */

                if(a[x]!=a[y])
                {
                    add(2*x,2*y+1);
                    add(2*y,2*x+1);
                }
                if(a[x]!=b[y])
                {
                    add(2*x,2*y);
                    add(2*y+1,2*x+1);
                }
                if(b[x]!=a[y])
                {
                    add(2*x+1,2*y+1);
                    add(2*y,2*x);
                }
                if(b[x]!=b[y])
                {
                    add(2*x+1,2*y);
                    add(2*y+1,2*x);
                }

            }
            else if(z==1)
            {
                /*           //这种加边方法也可以
                if(a[x]==a[y]&&b[x]==b[y])
                {
                    adj[2*x].push_back(2*y+1);
                    adj[2*y].push_back(2*x+1);
                    adj[2*x+1].push_back(2*y);
                    adj[2*y+1].push_back(2*x);

                    radj[2*x+1].push_back(2*y);
                    radj[2*y+1].push_back(2*x);
                    radj[2*x].push_back(2*y+1);
                    radj[2*y].push_back(2*x+1);
                }
                else if(a[x]==a[y]&&b[x]!=b[y])
                {
                    adj[2*x].push_back(2*y+1);
                    adj[2*y].push_back(2*x+1);
                    radj[2*y+1].push_back(2*x);
                    radj[2*x+1].push_back(2*y);
                }
                else if(a[x]!=a[y]&&b[x]==b[y])
                {
                    adj[2*x+1].push_back(2*y);
                    adj[2*y+1].push_back(2*x);
                    radj[2*y].push_back(2*x+1);
                    radj[2*x].push_back(2*y+1);
                }
                else if(a[x]==b[y])
                {
                    adj[2*x].push_back(2*y);
                    adj[2*y+1].push_back(2*x+1);
                    radj[2*y].push_back(2*x);
                    radj[2*x+1].push_back(2*y+1);
                }
                else if(b[x]==a[y])
                {
                    adj[2*x+1].push_back(2*y+1);
                    adj[2*y].push_back(2*x);
                    radj[2*y+1].push_back(2*x+1);
                    radj[2*x].push_back(2*y);
                }
                */

                if(a[x]==a[y])
                {
                    add(2*x,2*y+1);
                    add(2*y,2*x+1);
                }
                if(a[x]==b[y])
                {
                    add(2*x,2*y);
                    add(2*y+1,2*x+1);
                }
                if(b[x]==a[y])
                {
                    add(2*x+1,2*y+1);
                    add(2*y,2*x);
                }
                if(b[x]==b[y])
                {
                    add(2*x+1,2*y);
                    add(2*y+1,2*x);
                }

            }
        }
        if(!flag)
        {
            printf("Case #%d: no\n",iCase);
            continue;
        }
        korasaju();
        if(solvable())printf("Case #%d: yes\n",iCase);
        else printf("Case #%d: no\n",iCase);
    }
    return 0;
}

 

 

 

posted on 2012-10-07 13:46  kuangbin  阅读(1341)  评论(1编辑  收藏  举报

导航

JAVASCRIPT: