Eliminate the Conflict
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1675 Accepted Submission(s): 709
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?
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.
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
two-sat 好题 , two-sat考的就是构图~
Alice 跟 Bob 在玩剪刀石头布。
然后给出 Bob 出拳的序列 。
然后再对Alice的出拳有限制规则 。
x , y , 1 表示 , 第x回合跟第y回合不能出相同
x , y , 0 表示 , 第x回合跟第y回合一定要出相同。
问Alice能不能赢Bob .
我依然是构一个反向图。
猜拳猜输的情况不用考虑把2-sat 分为打平手跟胜利的两个集合是否会存在冲突。
#include <iostream> #include <vector> #include <queue> #include <cstring> #include<cstdio> using namespace std; typedef long long ll; const int N = 10010; int n , m ; int e[N]; int eh[N<<2], et[N<<6] , nxt[N<<6], tot ; bool mark[N<<2]; int st[N<<2] , top ; void init(){ tot = 0 ; memset( mark , false , sizeof mark ); memset( eh , -1 ,sizeof eh ); } void addedge( int u , int v ){ et[tot] = v , nxt[tot] = eh[u] ,eh[u] = tot ++ ; et[tot] = u , nxt[tot] = eh[v] ,eh[v] = tot ++ ; } bool dfs( int u ){ if( mark[u] ) return true ; if( mark[u^1] ) return false ; mark[u] = true ; st[ top++ ] = u ; for( int i = eh[u] ; ~i ; i = nxt[i] ){ int v = et[i]; if( !dfs( v^1 ) ) return false; } return true; } bool solve() { for( int i = 0 ; i < 2 * n ; i += 2 ){ if( !mark[i] && !mark[i+1] ){ top = 0 ; if( !dfs(i) ){ while( top > 0 ) mark[ st[ --top ] ] = false ; if( !dfs(i+1) ) return false ; } } } return true; } void run() { int op , x , y ; init(); scanf("%d%d",&n,&m); for(int i = 0 ; i < n ; ++i ){ scanf("%d",&e[i]); } while( m-- ){ scanf("%d%d%d",&x,&y,&op); x-- , y-- ; if( !op ){ if( e[x] == e[y] ){ addedge( 2*x , 2*y^1 ); addedge( 2*x^1 , 2*y ); } else { addedge(2*x , 2*y ); addedge(2*x^1 , 2*y^1 ); if( e[x] == 1 && e[y] == 2 ){ addedge( 2*x^1 , 2*y ); } else if( e[x] == 1 && e[y] == 3 ){ addedge( 2*x , 2*y^1 ); } else if( e[x] == 2 && e[y] == 1 ){ addedge( 2*x , 2*y^1 ); } else if( e[x] == 2 && e[y] == 3 ){ addedge( 2*x^1 , 2*y ); } else if( e[x] == 3 && e[y] == 1 ){ addedge( 2*x^1 , 2*y ); } else if( e[x] == 3 && e[y] == 2 ){ addedge( 2*x , 2*y^1 ); } } } else { if( e[x] == e[y] ){ addedge(2*x,2*y); addedge(2*x^1,2*y^1); } else { if( e[x] == 1 && e[y] == 2 ){ addedge( 2*x , 2*y^1 ); } else if( e[x] == 1 && e[y] == 3 ){ addedge( 2*x^1 , 2*y ); } else if( e[x] == 2 && e[y] == 1 ){ addedge( 2*x^1 , 2*y ); } else if( e[x] == 2 && e[y] == 3 ){ addedge( 2*x , 2*y^1 ); } else if( e[x] == 3 && e[y] == 1 ){ addedge( 2*x , 2*y^1 ); } else if( e[x] == 3 && e[y] == 2 ){ addedge( 2*x^1 , 2*y ); } } } } if( solve() ) puts("yes"); else puts("no"); } int main() { #ifdef LOCAL freopen("in","r",stdin); #endif // LOCAL int _ , cas = 1 ; scanf("%d",&_); while(_--) { printf("Case #%d: ",cas++); run(); } }