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 ...
随机推荐
- 一点一滴完全突破KAZE特征检测算法,从各向异性扩散滤波开始(1)
ECCV2012中出现了一种比SIFT更稳定的特征检测算法KAZE.尽管,这个算法是几个法国人提出的,但是算法却有一个日文的名字.KAZE是日语‘风’的谐音,最近宣布退休的宫崎骏所拍摄的影片“起风了” ...
- [置顶] android之Notification版本兼容性问题
首先先来创建一个notification提示 //概要 String tickerText = context.getResources().getText(R.string.app_name).to ...
- Swift - 生成各种控件的工厂类(包含标签,按钮,输入框等)
在iOS开发中,页面里有时会大量的用到一些控件,如果要一个个单独创建再设置样式的话就显得很麻烦.我们可以创建一个生成各种控件的工厂类,这样在需要的时候调用下就可以了. 下面以一个自定义的工厂类为例,其 ...
- WebApp中调试jsavascript
app有时候会使用网页作为一些功能页面,好处自然是有很多,但是也得调试 1 android app调试 自然是chrome remote debug 在android里面自带的浏览器一般都是基于chr ...
- QT源码分析(从QApplication开始)
QT源码分析 转载自:http://no001.blog.51cto.com/1142339/282130 今天,在给同学讲东西的时候,谈到了Qt源代码的问题,才发现自己对Qt机制的了解是在太少了,而 ...
- Qt制作应用插件
在Qt下,插件有两种形式,一种是用于QtCreator下,扩展IDE功能.另一种是用于扩展开发者的应用.本文要讲的是后者. 定义一个纯虚类作为插件接口 #include <QtPlugin> ...
- [iOS]简单的APP引导页的实现 (Swift)
在第一次打开APP或者APP更新后通常用引导页来展示产品特性 我们用NSUserDefaults类来判断程序是不是第一次启动或是否更新,在AppDelegate.swift中加入以下代码: func ...
- javascript (二) 事件
<script></script> 函数写法: function fun_name(){ x=docment.getElementById("demo") ...
- ACE的构建(VC++6.0环境)
ACE的构建(VC++6.0环境)Windows下ACE的构建1. 将ACE-5.5.zip解压到所需的安装目录,此处以E:/为例,解压后形成ACE_wrappers文件夹,因此ACE将会存在于ACE ...
- 两个Hacker,专门Patch TObject
http://hallvards.blogspot.fr/2006/03/hack-8-explicit-vmt-calls.html http://www.deltics.co.nz/blog/po ...