Eliminate the Conflict

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1315    Accepted Submission(s): 563
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


题意:
Alice和Bob玩石头、剪刀、布,玩n局,Alice已知Bob的n局的出法,Bob给了Alice m个限制第i、j两次必须相同或不同,问Alice能否不输。


思路:
已知Bob的出法后,保证Alice不输,那每局Alice就有两种出法,问题就变为2-sat了,根据Alice每局的出法,给出限制后找到约束条件建边就够了。注意挖掘隐含的约束条件,如i、j相同,而Bob在i、j出法不同,那么Alice在i中的选择与在j中的选择只有一个交集,必须选择这个交集,选了其他的就错了。(我就卡在这个地方了)

代码:
#include <cstdio>
#include <cstring>
#define maxn 20005
#define MAXN 100005
using namespace std; int n,m,num,flag;
int bob[maxn];
int head[maxn];
int scc[maxn];
int vis[maxn];
int stack1[maxn];
int stack2[maxn];
struct edge
{
int v,next;
} g[MAXN];
int a[4][2]=
{
0,0,
1,2,
2,3,
1,3
}; void init()
{
memset(head,0,sizeof(head));
memset(vis,0,sizeof(vis));
memset(scc,0,sizeof(scc));
stack1[0] = stack2[0] = num = 0;
flag = 1;
}
void addedge(int u,int v)
{
num++;
g[num].v = v;
g[num].next = head[u];
head[u] = num;
}
void dfs(int cur,int &sig,int &cnt)
{
if(!flag) return;
vis[cur] = ++sig;
stack1[++stack1[0]] = cur;
stack2[++stack2[0]] = cur;
for(int i = head[cur]; i; i = g[i].next)
{
if(!vis[g[i].v]) dfs(g[i].v,sig,cnt);
else
{
if(!scc[g[i].v])
{
while(vis[stack2[stack2[0]]] > vis[g[i].v])
stack2[0] --;
}
}
}
if(stack2[stack2[0]] == cur)
{
stack2[0] --;
++cnt;
do
{
scc[stack1[stack1[0]]] = cnt;
int tmp = stack1[stack1[0]];
if((tmp >n && scc[tmp - n] == cnt) || (tmp <= n && scc[tmp + n] == cnt)) // 注意这里的‘=’号
{
flag = false;
return;
}
}
while(stack1[stack1[0] --] != cur);
}
}
void Twosat()
{
int i,sig,cnt;
sig = cnt = 0;
for(i=0; i<n+n&&flag; i++)
{
if(!vis[i]) dfs(i,sig,cnt);
}
}
int main()
{
int i,j,t,u,v,k,test=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
num=0;
for(i=1; i<=n; i++)
{
scanf("%d",&bob[i]);
}
for(i=1; i<=m; i++)
{
scanf("%d%d%d",&u,&v,&k);
if(k)
{
if(a[bob[u]][0]==a[bob[v]][0]) addedge(u,v+n),addedge(v,u+n);
if(a[bob[u]][0]==a[bob[v]][1]) addedge(u,v),addedge(v+n,u+n);
if(a[bob[u]][1]==a[bob[v]][0]) addedge(u+n,v+n),addedge(v,u);
if(a[bob[u]][1]==a[bob[v]][1]) addedge(u+n,v),addedge(v+n,u);
}
else
{
if(bob[u]==bob[v])
{
addedge(u,v);
addedge(v,u);
addedge(u+n,v+n);
addedge(v+n,u+n);
}
else
{ //注意这里 只能唯一的选择一条边等价于不能选其他边
if(a[bob[u]][0]==a[bob[v]][0]) addedge(u+n,u),addedge(v+n,v);
if(a[bob[u]][0]==a[bob[v]][1]) addedge(u+n,u),addedge(v,v+n);
if(a[bob[u]][1]==a[bob[v]][0]) addedge(u,u+n),addedge(v+n,v);
if(a[bob[u]][1]==a[bob[v]][1]) addedge(u,u+n),addedge(v,v+n);
}
}
}
Twosat();
printf("Case #%d: ",++test);
if(flag) printf("yes\n");
else printf("no\n");
}
return 0;
}







 

hdu 4115 Eliminate the Conflict ( 2-sat )的更多相关文章

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

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

  2. 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 ...

  3. HDU 4115 Eliminate the Conflict

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

  4. hdu 4115 石头剪子布(2-sat问题)

    /* 意甲冠军:石头剪子布,目前已知n周围bob会有什么,对alice限制.供u,v,w:设w=0说明a,b回合必须出的一样 否则,必须不一样.alice假设输一回合就输了,否则就赢了 解: 2-sa ...

  5. HDU 5783 Divide the Sequence(数列划分)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  6. HDU 5795 A Simple Nim(简单Nim)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. HDU 3496 Watch The Movie(看电影)

    HDU 3496 Watch The Movie(看电影) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] New sem ...

  8. HDU 5224 Tom and paper(最小周长)

    HDU 5224 Tom and paper(最小周长) Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d &a ...

  9. HDU 5868 Different Circle Permutation(burnside 引理)

    HDU 5868 Different Circle Permutation(burnside 引理) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=586 ...

随机推荐

  1. 用canvas实现图片滤镜效果

    1.灰度效果 图片过滤效果之灰度效果 算法及原理: .299 * r + .587 * g + .114 * b; 2.油画效果 算法及原理: 用当前点四周一定范围内任意一点的颜色来替代当前点颜色,最 ...

  2. 剑指offer—第三章高质量代码(o(1)时间删除链表节点)

    题目:给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间删除该节点,链表节点与函数的定义如下:struct ListNode{int m_nValue;ListNode* m_pValue ...

  3. 【转】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型

    原文网址:http://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/ #!/usr/bin/python 是用 ...

  4. Linux/Unix shell sql 之间传递变量

    灵活结合Linux/Unix Shell 与SQL 之间的变量传输,极大程度的提高了DBA的工作效率,本文针对Linux/Unix shell sql 之间传递变量给出几个简单的示例以供参考. Lin ...

  5. javac 命令用法

    引用自己写的Class 在java中手动编译时,总提示找不到类,调试成功后,特把目录结构与编译成功的命令列出: 样例一: 文件名 MessageStore.java Hello.java 源码 pac ...

  6. 构建 XCache 的基本步骤

    构建 XCache 的基本步骤 在开始之前,首先确保 PHP 正常安装并核实 phpize 是否位于 shell 的 PATH 下.同时,还需要一个 C 编译器,例如 GNU Compiler Col ...

  7. SQL0668N 由于表 "db2inst1.test" 上的原因代码 "3",所以不允许操作(解因为LOAD引起的LOAD暂挂状态锁)

    DB2解因为LOAD引起的LOAD暂挂状态锁   一般解锁命名是,SET INTEGRITY FOR temp_test IMMEDIATE CHECKED   但是load暂挂状态是解不了的,可以l ...

  8. [Papers]MHD, $\p_3\pi$, Lebesgue space [Jia-Zhou, JMAA, 2012]

    $$\bex \p_3\pi\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=2,\quad 3\leq q\leq \infty. \ee ...

  9. Quartz使用总结

    废话的前言 以前凭借年轻,凡事都靠脑记.现在工作几年后发现,很多以前看过.用过的东西,再次拿起的时候总觉得记不牢靠."好记性不如烂笔头"应该是某位上了年纪的大叔的切肤之痛(仅次于上 ...

  10. Topogun教学视频

    http://www.iqiyi.com/w_19rrfss6dd.html http://www.iqiyi.com/w_19rrfsvo3h.html http://www.iqiyi.com/w ...