admin管理员组

文章数量:1596326

 

Eliminate the Conflict

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


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     比赛的时候没有看这题,比赛结束后看题目感觉是3-sat。 可是3-sat问题是NP完全的,没有线性算法。 看了官方解题报告说是2-sat。想想确实如此,其实只有两种选择而不是三种选择:平局或者胜利。 可是建图很纠结,一直不知道怎么建图。直到看了 http://blog.csdn/wsniyufang/article/details/6955738 这个建图方式才明白。 在2-sat中,i和j冲突,连两条有向边i-->j',j-->i'。 那么当i和j要求不同时,如果i和j相同,就表示i和j冲突,连两条有向边i-->j',j-->i'。 那么当i和j要求相同时,如果i和j不同,就表示i和j冲突,连两条有向边i-->j',j-->i'。 求强连通分量,如果i和i'在同一SCC中,则无解,输出no,否则输出yes。   代码:
#include<cstdio>
#include<cstring>
#include<vector>
#define N 20005
using namespace std;

int n,m,t,tt,num,adj[N];
int a[2][N],scc[N],stk[N],stk2[N],dfn[N];
struct edge
{
    int v,pre;
}e[N];
void insert(int u,int v)
{
    e[num].v=v;
    e[num].pre=adj[u];
    adj[u]=num++;
}
void dfs(int cur,int& sig,int& num)
{
    int i,v;
    dfn[cur]=++sig;
    stk[++stk[0]]=stk2[++stk2[0]]=cur;
    for(i=adj[cur];~i;i=e[i].pre)
        if(0==dfn[v=e[i].v])
            dfs(v,sig,num);
        else if(0==scc[v])
            while(dfn[stk2[stk2[0]]]>dfn[v])
                stk2[0]--;
    if(stk2[stk2[0]]==cur)
    {
        stk2[0]--;
        num++;
        do
        {scc[stk[stk[0]]]=num;}while(stk[stk[0]--]!=cur);
    }
}
int Gabow()
{
    int i,sig,num;
    sig=num=stk[0]=stk2[0]=0;
    memset(scc,0,sizeof(scc));
    memset(dfn,0,sizeof(dfn));
    for(i=0;i<n*2;i++)
        if(0==dfn[i])
            dfs(i,sig,num);
    return num;
}
bool check()
{
    for(int i=0;i<n*2;i+=2)
        if(scc[i]==scc[i+1])
            return 0;
    return 1;
}
int main()
{
    int i,j,u,v,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[0][i]);
            a[0][i]--;
            a[1][i]=(a[0][i]+2)%3;
        }
        num=0;
        memset(adj,-1,sizeof(adj));
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&k);
            u--;v--;
            for(i=0;i<2;i++)
                for(j=0;j<2;j++)
                    if(k&&a[i][u]==a[j][v]||!k&&a[i][u]!=a[j][v])
                    {
                        insert(2*u+i,2*v+j^1);
                        insert(2*v+j,2*u+i^1);
                    }
        }
        Gabow();
        printf("Case #%d: ",++tt);
        if(check())
            puts("yes");
        else
            puts("no");
            
    }
}

本文标签: hduEliminateSatconflict