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?
 
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
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;
}

 

hdu4115 Eliminate the Conflict的更多相关文章

  1. HDU-4115 Eliminate the Conflict 2sat

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4115 题意:Alice和Bob玩猜拳游戏,Alice知道Bob每次会出什么,为了游戏公平,Bob对Al ...

  2. hdu 4115 Eliminate the Conflict ( 2-sat )

    Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. HDU 4115 Eliminate the Conflict(2-SAT)(2011 Asia ChengDu Regional Contest)

    Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...

  4. HDU 4115 Eliminate the Conflict(2-sat)

    HDU 4115 Eliminate the Conflict pid=4115">题目链接 题意:Alice和Bob这对狗男女在玩剪刀石头布.已知Bob每轮要出什么,然后Bob给Al ...

  5. 图论--2-SAT--HDU/HDOJ 4115 Eliminate the Conflict

    Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...

  6. hdu4115:Eliminate the Conflict

    n<=10000局剪刀石头布,对面第i局出Ai,m<=10000种对你出什么提出的要求:Xi Yi Wi 表示第Xi局和第Yi局,Wi=1:必须不同:Wi=0:必须相同,问是否存在你一局都 ...

  7. HDU 4115 Eliminate the Conflict

    2-SAT,拆成六个点. #include<cstdio> #include<cstring> #include<cmath> #include<stack& ...

  8. Eliminate the Conflict HDU - 4115(2-sat 建图 hhh)

    题意: 石头剪刀布 分别为1.2.3,有n轮,给出了小A这n轮出什么,然后m行,每行三个数a b k,如果k为0 表示小B必须在第a轮和第b轮的策略一样,如果k为1 表示小B在第a轮和第b轮的策略不一 ...

  9. 2-sat(石头、剪刀、布)hdu4115

    Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. Windows Azure 安全最佳实践 - 第 5 部分:基于Claim 的标识,单点登录

    基于Claim的身份标识是处理网站与 Web 服务的身份认证和访问一种简单而强大的方式,无论您是在本地工作还是面向云工作.您可以通过减少自定义实施和使用基于Claim的单一简化标识模型,创建更安全的应 ...

  2. [LeetCode][Java] 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  3. tomcat7 启动报错(转)

    不加载任何自己的项目启动即报错:   严重: Error deploying web application directory D:\tomcat7.0.30\webapps\docs java.l ...

  4. iOS UIButton详细介绍

    昨天,做了项目中的一点,觉得细节还是很重要的.像一个普通的UIButton,给它调试字体格式,大小什么的,确实是蛮耗时间的. 今天打算详细的归纳归纳.. typedef NS_ENUM(NSInteg ...

  5. openCV中cvSnakeImage()函数代码分析

    /*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...

  6. Php 解析XML文件

    Php 解析XML文件 Php 解析XML文件,仅供学习參考!演示样例代码例如以下: <?php header("Content-type: text/html; charset=ut ...

  7. Swift - 基本数据类型,及常/变量声明

    2015-01-08 14:59 发布:yuhang 浏览:434 下面是Swift中基本的数据类型介绍说明: 1,变量:使用var声明 1 var str:String = "hangge ...

  8. (二)----HTTP请求头与响应头

    一.HTTP头引入: 正确的设置HTTP头部信息有助于搜索引擎判断网页及提升网站访问速度.通常HTTP消息包括:客户机向服务器的请求消息和服务器向客户机的响应消 息.客户端向服务器发送一个请求,请求头 ...

  9. Android 实现自己定义多级树控件和全选与反选的效果

    博文開始之前,首先要感谢大牛:(lmj623565791),本博文是在其博文http://blog.csdn.net/lmj623565791/article/details/40212367基础上进 ...

  10. 跟我一起学extjs5(13--运行菜单命令在tabPanel中显示模块)

    跟我一起学extjs5(13--运行菜单命令在tabPanel中显示模块)         上面设计好了一个模块的主界面,以下通过菜单命令的运行来把这个模块增加到主界面其中. 在MainModule. ...