admin管理员组文章数量:1596344
Eliminate the Conflict
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2343 Accepted Submission(s): 1023
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
Recommend We have carefully selected several similar problems for you: 4119 4118 4112 4111 4117
题目大意:
两个人猜拳,一个人知道另一个人每次要出什么,但他必须在一些回合出的一样或在一些回合出的不一样,问他能不能一次都不输。
解题思路:
开始看这道题并不想2-SAT,因为每个回合有3种状态。不过由于知道对方出什么,自己每回合能出的只能二选一,这就成为了2-SAT。
这题的建图那些常见的公式并不符合,需要自己根据“必须”这个根本的概念来建(详见代码)。
AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))
const int MAXN=10000+3;
const int MAXV=MAXN*2;
vector<int> G[MAXV];
int N,M,a[MAXN],dfn[MAXV],low[MAXV],ndfn,vis[MAXV],belong[MAXV],ngruop;
stack<int> st;
void init()//初始化
{
for(int i=0;i<N*2;++i)
G[i].clear();
ndfn=ngruop=0;
mem(vis,0);
}
void tarjan(int u)//tarjan算法求强连通分量
{
dfn[u]=low[u]=++ndfn;
vis[u]=1;
st.push(u);
for(int i=0;i<G[u].size();++i)
{
int v=G[u][i];
if(!vis[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]==1)
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
++ngruop;
while(true)
{
int v=st.top(); st.pop();
vis[v]=2;
belong[v]=ngruop;
if(v==u)
break;
}
}
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=1;cas<=T;++cas)
{
scanf("%d%d",&N,&M);
init();
for(int i=0;i<N;++i)//第i个点表示a[i],第i+N个节点表示(a[i]+1)%3
{
scanf("%d",&a[i]);
--a[i];
}
for(int i=0;i<M;++i)
{
int x,y,op;
scanf("%d%d%d",&x,&y,&op);
--x;
--y;
if(a[x]==a[y])//这两个回合对方出的一眼
{
if(op)//要求出的不一样,x^y==1
{
G[x].push_back(y+N);
G[y].push_back(x+N);
G[x+N].push_back(y);
G[y+N].push_back(x);
}
else//要求出的一样,x^y==0
{
G[x].push_back(y);
G[y].push_back(x);
G[x+N].push_back(y+N);
G[y+N].push_back(x+N);
}
}
else//对方出的不一样
{
if(a[x]>a[y])
swap(x,y);
if(a[x]==0&&a[y]==2)
swap(x,y);
//a[x+N]与a[y]选择相同
if(op)
{
G[x+N].push_back(y+N);//一次出公共的,另一次必须出非公共的
G[y].push_back(x);
}
else
{
G[x].push_back(x+N);//必须都出公共的
G[y+N].push_back(y);
}
}
}
for(int i=0;i<N*2;++i)
if(!vis[i])
tarjan(i);
bool ok=true;
for(int i=0;i<N;++i)
if(belong[i]==belong[i+N])//出现矛盾
{
ok=false;
break;
}
printf("Case #%d: ",cas);
puts(ok?"yes":"no");
}
return 0;
}
版权声明:本文标题:HDU 4155 Eliminate the Conflict(2-SAT) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728255812a1150998.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论