Eliminate the Conflict

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1732    Accepted Submission(s): 751
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 B1,B2, ...,BN, where Bi represents what item Bob will play in the ith 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 Ath and Bth 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..
题意:两个人玩石头剪刀布,进行n轮比赛,首先给出B的这n轮的手势,然后m行对A的限制,每行a,b,c当c==0的时候代表A第a轮和第b轮的手势相同,c==1表示第a轮和第b轮的手势不同,如果A在任何一轮中输掉或者违反规定的话,A就是输的,问A有没有赢的可能;
分析:开始看的时候每轮A都是有3个选择,但是其实只有两个合法的选择,即平局和胜利的手势,所以应该是用2-sat判矛盾,主要是建图,当c==0时说明ab两轮的手势相同就是合法的,不同就是矛盾,对于矛盾建边,c==1的时候同理;
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
#include"algorithm"
#include"string.h"
#include"string"
#include"stack"
#include"map"
#define inf 0x3f3f3f3f
#define M 20009
using namespace std;
struct node
{
int u,v,next;
}edge[M*20];
stack<int>q;
int t,head[M],low[M],dfn[M],belong[M],num,index,use[M];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[t].u=u;
edge[t].v=v;
edge[t].next=head[u];
head[u]=t++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++index;
q.push(u);
use[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(use[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
num++;
int vv;
do
{
vv=q.top();
q.pop();
use[vv]=0;
belong[vv]=num;
}while(vv!=u);
}
}
int psq(int n)
{
int i;
num=index=n;
memset(use,0,sizeof(use));
memset(dfn,0,sizeof(dfn));
for(i=1;i<=2*n;i++)
if(!dfn[i])
tarjan(i);
for(i=1;i<=n;i++)
if(belong[i]==belong[i+n])
return 0;
return 1;
}
int a[M],b[M];
int main()
{
int T,m,n,i,u,v,w,kk=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
for(i=1;i<=n;i++)
{
scanf("%d",&b[i]);
a[i]=((b[i]+1)%3==0)?3:(b[i]+1)%3;
a[i+n]=b[i];
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
if(w==0)
{
if(a[u]!=a[v])
{
add(u,v+n);
add(v,u+n);
}
if(a[u]!=a[v+n])
{
add(u,v);
add(v+n,u+n);
}
if(a[u+n]!=a[v])
{
add(u+n,v+n);
add(v,u);
}
if(a[u+n]!=a[v+n])
{
add(u+n,v);
add(v+n,u);
}
}
else
{
if(a[u]==a[v])
{
add(u,v+n);
add(v,u+n);
}
if(a[u]==a[v+n])
{
add(u,v);
add(v+n,u+n);
}
if(a[u+n]==a[v])
{
add(u+n,v+n);
add(v,u);
}
if(a[u+n]==a[v+n])
{
add(u+n,v);
add(v+n,u);
}
}
}
printf("Case #%d: ",kk++);
if(psq(n))
printf("yes\n");
else
printf("no\n");
}
}

2-sat(石头、剪刀、布)hdu4115的更多相关文章

  1. 09_编写脚本,实现人机<石头,剪刀,布>游戏

    #!/bin/bashgame=(石头 剪刀 布)num=$[RANDOM%3]computer=${game[$num]}#通过随机数获取计算机的出拳#出拳的可能性保存在一个数组中,game[0], ...

  2. Python 石头 剪刀 布

    di = {1: '石头', 2: '剪刀', 3: '布'} def win(x, y): if len({x[0], y[0]}) == 1: print('平局.') else: if {x[0 ...

  3. 用 Python 编写剪刀、石头、布的小游戏(快速学习python语句)

    import random#定义手势类型allList = ['石头','剪刀','布']#定义获胜的情况winList = [['石头','剪刀'],['剪刀','布'],['步','石头']]pr ...

  4. Java猜拳小游戏(剪刀、石头、布)

    1.第一种实现方法,调用Random数据包,直接根据“1.2.3”输出“剪刀.石头.布”.主要用了9条输出判断语句. import java.util.Random; import java.util ...

  5. Java自制人机小游戏——————————剪刀、石头、布

    package com.hello.test; import java.util.Scanner; public class TestGame { public static void main(St ...

  6. 用Micro:bit做剪刀、石头、布游戏

    剪刀.石头.布游戏大家都玩过,今天我们用Micro:bit建一个剪刀.石头.布游戏! 第一步,起始 当你摇动它时,我们希望the micro:bit选择剪刀.石头.布.尝试创建一个on shake b ...

  7. PAT(B) 1018 锤子剪刀布(C:20分,Java:18分)

    题目链接:1018 锤子剪刀布 分析 用一个二维数组保存两人所有回合的手势 甲乙的胜,平,负的次数刚好相反,用3个变量表示就可以 手势单独保存在signs[3]中,注意顺序.题目原文:如果解不唯一,则 ...

  8. PAT (Basic Level) Practise:1018. 锤子剪刀布

    [题目链接] 大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入格式: 输入第1行 ...

  9. PAT乙级 1018. 锤子剪刀布 (20)

    1018. 锤子剪刀布 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 大家应该都会玩“锤子剪刀布”的游 ...

  10. PAT-乙级-1018. 锤子剪刀布 (20)

    1018. 锤子剪刀布 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 大家应该都会玩“锤子剪刀布”的游 ...

随机推荐

  1. 如何更改Magento的Base URL

    Magento的Base URL是用于访问商店页面的URL,您也可以为单独一个store view设置一个Base Url.在改这项值之前请确保您的域名已经指向了网站所在服务器的IP,DNS解析完成后 ...

  2. day01-基础内容

    day01-基础内容 1.Linux:  1)开源的操作系统.免费的    主要用于服务器端,而Java主要是服务器端开发  2)Linux与Windows目录结构的区别:    2.1)文件系统不同 ...

  3. 高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群

    高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群 libnet软件包<-依赖-heartbeat(包含ldirectord插件(需要perl-MailTools的rpm包)) l ...

  4. PHP受保护的类和私有类什么区别

    受保护的继承后可以访问,私有的只能在该类中访问,不会被继承访问class Man{ protected $name='lee';//受保护 private $age=123;//私有 function ...

  5. Sublime text 3 SVN插件及使用方法

    前提是你电脑里已装有SVN,这时只是设置快捷调用SVN. 1.通过快捷键 ctrl+` 或者 View > Show Console 菜单打开控制台 2.粘贴对应版本的代码后回车安装 适用于 S ...

  6. iOS - (TableView中利用系统的 cell 设置 cell.textlabel 位置和大小)

    今天工作稍微的遇到了一点小小的难题,需求效果中 TableView cell 中的 Label 字体大小比原先系统中的要大些且 Label 位置不是在前面,而是在中间往后,对于这个问题我第一时间也是想 ...

  7. 离线下载Windows 调试符号 Symbols

    公司开发机没有不能连接到互联网.调试程序时那些Windows模块(如ntdll.dll)不能加载符号,而程序总是崩在这些模块里.想看一眼到底崩在了什么地方. 需要把对应的符号下载下来. 使用工具sym ...

  8. TOM大叔的几道Javascript题目与解答

    几道JS题目 之前没有深入研究js语言,最近几年前端越来越工程化,需要扎实的js基础,看到博客园上有很多大牛分享JS学习文章,幸运看到tom大叔的blog,抽时间潜心学习了其文章,遇到到其出的几道题目 ...

  9. pip是用国内镜像源

    pipy国内镜像目前有: http://pypi.douban.com/  豆瓣 http://pypi.hustunique.com/  华中理工大学 http://pypi.sdutlinux.o ...

  10. Excessive AWR Growth From Partitioned Objects Such as SYS.WRH$_EVENT_HISTOGRAM Causing Sysaux to Grow

    AWR数据增长较快,导致sysaux表空间使用较高 SQL> select f.tablespace_name, 2 a.total, 3 f.free, 4 round((f.free / a ...