HDU 4115 Eliminate the Conflict (2-SAT)
22929 ワード
Eliminate the Conflict
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 740 Accepted Submission(s): 301
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
タイトル:
2人で石ハサミの布のゲームをして、2人は連続してN輪を游んで、その中に与えます
一人のNラウンドの状況とその人が他の人に対するいくつかの制限条件には、2つの制限があります.
各制限は、(a,b,c)であり、c=0であれば、その人が他の人に対する制限がa番目であることを示す.
局は第b局と同じであるべきで、もしc=1が異なっていることを表すならば、別の人に勝つことがあるかどうかを聞きます
かもしれません.
2-SATのテーマは見ただけで分かりました.二つの選択は、矛盾する場合がある.
主に図を建てるので、図を建てる過程は細心の注意を払って、間違いやすいです.
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 740 Accepted Submission(s): 301
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
タイトル:
2人で石ハサミの布のゲームをして、2人は連続してN輪を游んで、その中に与えます
一人のNラウンドの状況とその人が他の人に対するいくつかの制限条件には、2つの制限があります.
各制限は、(a,b,c)であり、c=0であれば、その人が他の人に対する制限がa番目であることを示す.
局は第b局と同じであるべきで、もしc=1が異なっていることを表すならば、別の人に勝つことがあるかどうかを聞きます
かもしれません.
2-SATのテーマは見ただけで分かりました.二つの選択は、矛盾する場合がある.
主に図を建てるので、図を建てる過程は細心の注意を払って、間違いやすいです.
/*
HDU 4115
:
, N ,
N , :
:(a,b,c) , c==0 a
b , c==1 ,
。
*/
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
const int MAXN=22000;//
bool visit[MAXN];
queue<int>q1,q2;
//vector
vector<vector<int> >adj; // // '>'
vector<vector<int> >radj;//
vector<vector<int> >dag;// DAG
int n,m,cnt;
int id[MAXN],order[MAXN],ind[MAXN];// , ,
void dfs(int u)
{
visit[u]=true;
int i,len=adj[u].size();
for(i=0;i<len;i++)
if(!visit[adj[u][i]])
dfs(adj[u][i]);
order[cnt++]=u;
}
void rdfs(int u)
{
visit[u]=true;
id[u]=cnt;
int i,len=radj[u].size();
for(i=0;i<len;i++)
if(!visit[radj[u][i]])
rdfs(radj[u][i]);
}
void korasaju()
{
int i;
memset(visit,false,sizeof(visit));
for(cnt=0,i=0;i<2*n;i++)
if(!visit[i])
dfs(i);
memset(id,0,sizeof(id));
memset(visit,false,sizeof(visit));
for(cnt=0,i=2*n-1;i>=0;i--)
if(!visit[order[i]])
{
cnt++;//
rdfs(order[i]);
}
}
bool solvable()
{
for(int i=0;i<n;i++)
if(id[2*i]==id[2*i+1])
return false;
return true;
}
void add(int x,int y)
{
adj[x].push_back(y);
radj[y].push_back(x);
}
int a[MAXN],b[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int x,y,z;
scanf("%d",&T);
int iCase=0;
while(T--)
{
iCase++;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]==1)b[i]=2;
else if(a[i]==2)b[i]=3;
else b[i]=1;
if(a[i]>b[i])swap(a[i],b[i]);
}
adj.assign(2*n,vector<int>());
radj.assign(2*n,vector<int>());
bool flag=true;
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
x--;
y--;
if(!flag)continue;
if(x==y)
{
if(z==1)flag=false;
continue;
}
if(z==0)//
{
/* //
if(a[x]==a[y]&&b[x]==b[y])
{
adj[2*x].push_back(2*y);
adj[2*y].push_back(2*x);
adj[2*x+1].push_back(2*y+1);
adj[2*y+1].push_back(2*x+1);
radj[2*x].push_back(2*y);
radj[2*y].push_back(2*x);
radj[2*x+1].push_back(2*y+1);
radj[2*y+1].push_back(2*x+1);
}
else if(a[x]==a[y]&&b[x]!=b[y])// a
{
adj[2*x+1].push_back(2*x);
adj[2*y+1].push_back(2*y);
radj[2*x].push_back(2*x+1);
radj[2*y].push_back(2*y+1);
}
else if(a[x]!=a[y]&&b[x]==b[y])// b
{
adj[2*x].push_back(2*x+1);
adj[2*y].push_back(2*y+1);
radj[2*x+1].push_back(2*x);
radj[2*y+1].push_back(2*y);
}
else if(a[x]==b[y])//23 12
{
adj[2*x+1].push_back(2*x);
adj[2*y].push_back(2*y+1);
radj[2*x].push_back(2*x+1);
radj[2*y+1].push_back(2*y);
}
else if(b[x]==a[y])
{
adj[2*x].push_back(2*x+1);
adj[2*y+1].push_back(2*y);
radj[2*x+1].push_back(2*x);
radj[2*y].push_back(2*y+1);
}
*/
if(a[x]!=a[y])
{
add(2*x,2*y+1);
add(2*y,2*x+1);
}
if(a[x]!=b[y])
{
add(2*x,2*y);
add(2*y+1,2*x+1);
}
if(b[x]!=a[y])
{
add(2*x+1,2*y+1);
add(2*y,2*x);
}
if(b[x]!=b[y])
{
add(2*x+1,2*y);
add(2*y+1,2*x);
}
}
else if(z==1)
{
/* //
if(a[x]==a[y]&&b[x]==b[y])
{
adj[2*x].push_back(2*y+1);
adj[2*y].push_back(2*x+1);
adj[2*x+1].push_back(2*y);
adj[2*y+1].push_back(2*x);
radj[2*x+1].push_back(2*y);
radj[2*y+1].push_back(2*x);
radj[2*x].push_back(2*y+1);
radj[2*y].push_back(2*x+1);
}
else if(a[x]==a[y]&&b[x]!=b[y])
{
adj[2*x].push_back(2*y+1);
adj[2*y].push_back(2*x+1);
radj[2*y+1].push_back(2*x);
radj[2*x+1].push_back(2*y);
}
else if(a[x]!=a[y]&&b[x]==b[y])
{
adj[2*x+1].push_back(2*y);
adj[2*y+1].push_back(2*x);
radj[2*y].push_back(2*x+1);
radj[2*x].push_back(2*y+1);
}
else if(a[x]==b[y])
{
adj[2*x].push_back(2*y);
adj[2*y+1].push_back(2*x+1);
radj[2*y].push_back(2*x);
radj[2*x+1].push_back(2*y+1);
}
else if(b[x]==a[y])
{
adj[2*x+1].push_back(2*y+1);
adj[2*y].push_back(2*x);
radj[2*y+1].push_back(2*x+1);
radj[2*x].push_back(2*y);
}
*/
if(a[x]==a[y])
{
add(2*x,2*y+1);
add(2*y,2*x+1);
}
if(a[x]==b[y])
{
add(2*x,2*y);
add(2*y+1,2*x+1);
}
if(b[x]==a[y])
{
add(2*x+1,2*y+1);
add(2*y,2*x);
}
if(b[x]==b[y])
{
add(2*x+1,2*y);
add(2*y+1,2*x);
}
}
}
if(!flag)
{
printf("Case #%d: no
",iCase);
continue;
}
korasaju();
if(solvable())printf("Case #%d: yes
",iCase);
else printf("Case #%d: no
",iCase);
}
return 0;
}