hdu4115 Eliminate the Conflict
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
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?
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.
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
Case #2: yes
'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..
#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的更多相关文章
- HDU-4115 Eliminate the Conflict 2sat
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4115 题意:Alice和Bob玩猜拳游戏,Alice知道Bob每次会出什么,为了游戏公平,Bob对Al ...
- hdu 4115 Eliminate the Conflict ( 2-sat )
Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 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 ...
- HDU 4115 Eliminate the Conflict(2-sat)
HDU 4115 Eliminate the Conflict pid=4115">题目链接 题意:Alice和Bob这对狗男女在玩剪刀石头布.已知Bob每轮要出什么,然后Bob给Al ...
- 图论--2-SAT--HDU/HDOJ 4115 Eliminate the Conflict
Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...
- hdu4115:Eliminate the Conflict
n<=10000局剪刀石头布,对面第i局出Ai,m<=10000种对你出什么提出的要求:Xi Yi Wi 表示第Xi局和第Yi局,Wi=1:必须不同:Wi=0:必须相同,问是否存在你一局都 ...
- HDU 4115 Eliminate the Conflict
2-SAT,拆成六个点. #include<cstdio> #include<cstring> #include<cmath> #include<stack& ...
- 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轮的策略不一 ...
- 2-sat(石头、剪刀、布)hdu4115
Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- JQuery - 留言之后,不重新加载数据,直接显示发表内容
留言板中,发表信息的时候,使用Ajax存储到后台数据库,如果存储成功,不重新加载数据库,直接显示发表内容. 代码: var Nicehng = ''; var kkimgpath = ''; var ...
- JDK1.5后的新特性之一:可变参数
Java中的可变参数 Java1.5后出现了一个新特性,即可变参数,格式为:类型 …参数 例如: 总的来说,可变参数可以当成是数组来用: public void testSum() { System. ...
- 浅谈MySQL 数据库性能优化
MySQL数据库是 IO 密集型的程序,和其他数据库一样,主要功能就是数据的持久化以及数据的管理工作.本文侧重通过优化MySQL 数据库缓存参数如查询缓存,表缓存,日志缓存,索引缓存,innodb缓存 ...
- hdc和hwnd的区别
句柄概念在WINDOWS编程中是一个很重要的概念,在许多地方都扮演着重要的角色.但由此而产生的句柄概念也大同小异,比如:<<Microsoft Windows 3 Develo ...
- nohup命令与&区别,jobs,fg,bg,Ctrl-Z、Ctrl-C、Ctrl-D
&方式: Unix/Linux下一般想让某个程序在后台运行,很多都是使用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台: /usr/local/my ...
- vc2010下使用64位控件
最近把我的控件(ST_Curve www.st-curve.cn)升级到了64位,2010编译,本来以为很简单的问题,结果折腾了两天(也有可能我多年没做过界面和vc相关的东西了吧),于是把我遇到的问题 ...
- Datatable.Compute小技巧
在个人版机房重构的过程中,大家最发愁的一件事无非就是上下机,结账和报表.那么在结账的过程中,最发愁的是否就数计算日结账单的数据和周结账的数据.还记得在第一遍机房收费系统的过程中用的是for 循环,但是 ...
- HDU 1269 迷宫城堡(强连通)
HDU 1269 迷宫城堡 pid=1269" target="_blank" style="">题目链接 题意:中文题 思路:强连通模板题 代 ...
- win32 sdk树形控件的项拖拽实现
本课中,我们将学习如何使用树型视图控件.另外还要学习如何在树型视图中完成拖-拉动作,以及如何使用图象列表. 理论: 树型视图是一种特别的窗口,我们可以使用它一目了然地表示某种层次关系.譬如象在资源管理 ...
- HDU--杭电--4502--吉哥系列故事——临时工计划--背包--01背包
吉哥系列故事——临时工计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) To ...