Eliminate the Conflict
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1114 Accepted Submission(s): 468
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
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
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
2sat题,主要是建图,不好建,一般的2sat题都是要找到所有矛盾,然后,反向两个建边就可以了 !
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
using namespace std;
#define N 40080 /*顶点总数,包括拆点以后的*/
#define inf 0x4f4f4f4f
vector<int>g[N]; /*邻接表*/
int n, m; /*顶点数,边数*/
int id[N], pre[N], low[N], s[N], stop, cnt, scnt,pa[N],pb[N][2];
bool flag=true;
void tarjan(int v) { /*求强连通分量,vertex: 0 ~ n-1*/
int t, minc = low[v] = pre[v] = cnt++;
s[stop++] = v;
for (int i = 0; i < g[v].size(); i++) {
if (-1 == pre[g[v][i]]) tarjan(g[v][i]);
if (low[g[v][i]] < minc) minc = low[g[v][i]];
}
if (minc < low[v]) { low[v] = minc; return; }
do { t = s[--stop];id[t] = scnt; low[t] = N; } while (t != v);
++scnt; /*联通分量个数*/
}
int _2sat() {
stop = cnt = scnt = 0;
memset(pre, -1, sizeof (pre));
for (int i = 0; i <n+ n; ++i) if (-1 == pre[i]) tarjan(i);
for (int i = 0; i < n; i++) if (id[i] == id[i + n]) return 0;
return 1;
}
void build(int k) { /**/
for(int i=0;i<=n+n;i++)
g[i].clear();
for(int i=0;i<n;i++)
{
scanf("%d",&pa[i]);
if(pa[i]==1)
pb[i][0]=1,pb[i][1]=2;
else if(pa[i]==2)
pb[i][0]=2,pb[i][1]=3;
else if(pa[i]==3)
pb[i][0]=1,pb[i][1]=3;
} for(int i=0;i<k;i++)
{
int u,v,fl;
scanf("%d%d%d",&u,&v,&fl);
u--,v--;
if(fl==1)
{
if(pb[u][0]==pb[v][0])
{
g[u].push_back(v+n);
g[v].push_back(u+n);
}
if(pb[u][0]==pb[v][1])
{
g[u].push_back(v);
g[v+n].push_back(u+n);
}
if(pb[u][1]==pb[v][0])
{
g[u+n].push_back(v+n);
g[v].push_back(u);
}
if(pb[u][1]==pb[v][1])
{
g[u+n].push_back(v);
g[v+n].push_back(u);
}
}
else
{
if(pb[u][0]!=pb[v][0])
{
g[u].push_back(v+n);
g[v].push_back(u+n);
}
if(pb[u][0]!=pb[v][1])
{
g[u].push_back(v);
g[v+n].push_back(u+n);
}
if(pb[u][1]!=pb[v][0])
{
g[u+n].push_back(v+n);
g[v].push_back(u);
}
if(pb[u][1]!=pb[v][1])
{
g[u+n].push_back(v);
g[v+n].push_back(u);
}
}
} }
int main() {
int k,tt=1,tcase;
scanf("%d",&tcase);
while ( tcase--) {
scanf("%d%d", &n, &k); flag=true;
build(k);
int ans = _2sat();
if(ans){ /**/
printf("Case #%d: yes\n",tt++);
}
else{ /**/
printf("Case #%d: no\n",tt++);
}
}
return 0;
}