HDU 4115 Eliminate the Conflict (2-sat,4级)

N - Eliminate the Conflict
Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description:

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..
 
思路:不能出的建互斥关系,如出石头,则一定不出剪子。

            出一个的限制。出一样与不同,拆成石头剪子布,三点,一次一一对应就好了。


#include<cstdio>
#include<cstring>
#include<iostream>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=400010;
class Edge
{
  public:int v,next;
};
class TWO_SAT
{
public:
  int dfn[mm],e_to[mm],stack[mm];
  Edge e[mm*2];
  int edge,head[mm],top,dfs_clock,bcc;
  void clear()
  {
    edge=0;clr(head,-1);
  }
  void add(int u,int v)
  {
    e[edge].v=v;e[edge].next=head[u];head[u]=edge++;
  }
  void add_my(int x,int xval,int y,int yval)
  {
    x=x+x+xval;y=y+y+yval;
    add(x,y);
  }
  void add_clause(int x,int xval,int y,int yval)
  {///x or y
    x=x+x+xval;
    y=y+y+yval;
    add(x^1,y);add(y^1,x);
  }
  void add_con(int x,int xval)
  { x=x+x+xval;
    add(x^1,x);
  }
  int tarjan(int u)
  {
    int lowu,lowv;
    lowu=dfn[u]=++dfs_clock;
    int v; stack[top++]=u;
    for(int i=head[u];~i;i=e[i].next)
    {
      v=e[i].v;
      if(!dfn[v])
      {
        lowv=tarjan(v);
        lowu=min(lowv,lowu);
      }
      else if(e_to[v]==-1)//in stack
        lowu=min(lowu,dfn[v]);
    }
    if(dfn[u]==lowu)
    {
      ++bcc;
      do{
        v=stack[--top];
        e_to[v]=bcc;
      }while(v!=u);
    }
    return lowu;
  }
  bool find_bcc(int n)
  { clr(e_to,-1);
    clr(dfn,0);
    bcc=dfs_clock=top=0;
    FOR(i,0,2*n-1)
    if(!dfn[i])
      tarjan(i);
    for(int i=0;i<2*n;i+=2)
      if(e_to[i]==e_to[i^1])return 0;
    return 1;
  }
}two;
int n,m;
int main()
{ int a,b,c,d,cas;
  scanf("%d",&cas);
  FOR(ca,1,cas)
  { scanf("%d%d",&n,&m);
    two.clear();
    FOR(i,0,n-1)
    {
      scanf("%d",&a);
      --a;
      two.add_con(i*3+(a+2)%3,0);
      two.add_clause(i*3+a,1,i*3+(a+1)%3,1);
      two.add_clause(i*3+a,0,i*3+(a+1)%3,0);
    }
    FOR(i,1,m)
    {
      scanf("%d%d%d",&a,&b,&c);
      --a;--b;
      if(c)///diffrerent
      {two.add_clause(a*3,0,b*3,0);
       two.add_clause(a*3+1,0,b*3+1,0);
       two.add_clause(a*3+2,0,b*3+2,0);
      }
      else ///same
      {
        two.add_clause(a*3,0,b*3,1);
        two.add_clause(a*3,1,b*3,0);
        two.add_clause(a*3+1,0,b*3+1,1);
        two.add_clause(a*3+1,1,b*3+1,0);
        two.add_clause(a*3+2,0,b*3+2,1);
        two.add_clause(a*3+2,1,b*3+2,0);
      }
    }
    printf("Case #%d: ",ca);
    if(two.find_bcc(n*3))printf("yes\n");
    else printf("no\n");
  }
}




posted @ 2013-09-21 19:27  剑不飞  阅读(123)  评论(0编辑  收藏  举报