HDU 4115 Eliminate the Conflict 2-sat 建圖

                                           HDU 4115 Eliminate the Conflict

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


題意:題目是意思是兩個人猜拳,石頭剪刀布,n輪猜拳,Alice 知道Bob出拳規律,約定m個規則,每個規則有a,b,k. 代表:如果k == 0 ,Alice在第a次和第b次出拳必須是一樣的,如果k == 1, 則必須是不一樣的。Alice必須滿足所有規則並且沒有一輪輸纔算Alice最終贏,問Alice是否有機會贏。

題解:首先有由於已知Bob出拳順序,且Alice不能輸掉一輪,所以實際上Alice只有兩種選擇,但是這兩種選擇對於每一輪可能是不一樣的。
當k == 0 時,如果第a輪和第b輪的選擇是一樣的,那麼必須連邊 a-b, b-a, a^1-b^1, b^1-a^1。
如果選擇不一樣,則必有一個共同選擇,所以必須將a,b各輪不同的選擇連到相同的選擇,表示一旦選了不同的選擇,就會選到相同的選擇,這樣相當於短路性質,直接將不同的選擇給短路掉了。(這個性質很重要)。
當k == 1時,如果第a輪和第b輪的選擇是一樣的,那麼必須連邊 a-b^1, b-a^1, a^1-b, b^1-a。
如果選擇不一樣,就將a,b各輪相同的選擇連到對方不同的選擇。比如a : 1 2  b: 2 3, 連a2-b3  b2-a1 ,這樣就防止了同時選a2,b2,如果有a2-b2就會立即觸發各自的另一個選擇,從而返回false。

2-sat 要想清楚邏輯關係和應該要建的對應邊之間的聯繫。

 

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4*2+100;
int n,m;
vector <int> G[maxn];//正向鄰接表
vector <int> rG[maxn];//反向鄰接表
vector <int> vs;     //拓撲序存點
int cmp[maxn]; //標號
bool used[maxn];

//Kosaraju
void dfs(int x)
{
  used[x] = true;
  for(int i = 0; i < G[x].size(); i++)
   if(!used[G[x][i]])  dfs(G[x][i]);
  vs.push_back(x);
}

void rdfs(int x, int k)
{
  used[x] = true;
  cmp[x] = k;
  for(int i = 0; i < rG[x].size(); i++)
    if(!used[rG[x][i]]) rdfs(rG[x][i],k);
}

int scc()
{
  memset(used, false , sizeof(used));
  vs.clear();
  for(int i = 0; i < n*2; i++)
     if(!used[i]) dfs(i);
  memset(used, false ,sizeof(used));
  int k = 0;
  for(int i = vs.size()-1; i >= 0; i--)
     if(!used[vs[i]])  rdfs(vs[i],++k);
  return k;
}

void add_edge(int u, int v)
{
  G[u].push_back(v);
  rG[v].push_back(u);
  //cout << u << " - "<< v << endl;
}

int b[maxn];
int c[maxn][2];

int main()
{
  int T;
  int cs = 0;
  scanf("%d",&T);
  while(T--)
  {
    scanf("%d%d",&n,&m);
    bool t = true;
    for(int i = 0; i < 2*n; i++) {G[i].clear();rG[i].clear();}
    for(int i = 0; i < n; i++)
      {
        scanf("%d",&b[i]);
        if(b[i] == 1)  c[i][0] = 1,c[i][1] = 2;
        else if(b[i] == 2) c[i][0] = 2,c[i][1] = 3;
        else {c[i][0] = 1,c[i][1] = 3;}
      }
    for(int i = 0; i < m; i++)
    {
      int u,v,p;
      scanf("%d%d%d",&u,&v,&p);
      u--,v--;
      if(p == 0)
        {
          if(b[u] == b[v])
          {
            add_edge(u*2,v*2);
            add_edge(v*2,u*2);
            add_edge(u*2+1,v*2+1);
            add_edge(v*2+1,u*2+1);
          }
          else
          {
            if(c[u][0] == c[v][0]) {add_edge(u*2+1,u*2);add_edge(v*2+1,v*2);}
            if(c[u][0] == c[v][1]) {add_edge(u*2+1,u*2);add_edge(v*2,v*2+1);}
            if(c[u][1] == c[v][0]) {add_edge(u*2,u*2+1);add_edge(v*2+1,v*2);}
            if(c[u][1] == c[v][1]) {add_edge(u*2,u*2+1);add_edge(v*2,v*2+1);}
          }
        }
      else
      {
         if(b[u] == b[v])
         {
           add_edge(u*2,v*2+1);
           add_edge(v*2,u*2+1);
           add_edge(u*2+1,v*2);
           add_edge(v*2+1,u*2);
         }
         else
         {
           if(c[u][0] == c[v][0]) {add_edge(u*2,v*2+1);add_edge(v*2,u*2+1);}
           if(c[u][0] == c[v][1]) {add_edge(u*2,v*2);add_edge(v*2+1,u*2+1);}
           if(c[u][1] == c[v][0]) {add_edge(u*2+1,v*2+1);add_edge(v*2,u*2);}
           if(c[u][1] == c[v][1]) {add_edge(u*2+1,v*2);add_edge(v*2+1,u*2);}
         }
      }
    }
  scc();
  for(int i = 0; i < n; i++)
    if(cmp[i*2] == cmp[i*2+1]) {t = false;break;}
  printf("Case #%d: ",++cs);
  if(t) printf("yes\n");
  else printf("no\n");
 }
  return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章