HDU 4115 Eliminate the Conflict
2-SAT,拆成六个点。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=+; int T,N,M;
stack<int>S;
vector<int>G[maxn];
vector<int>FG[maxn];
int Belong[maxn];
int flag[maxn];
int Block; int X[maxn]; void init()
{
for(int i=; i<maxn; i++) G[i].clear();
for(int i=; i<maxn; i++) FG[i].clear();
memset(Belong,,sizeof Belong);
memset(flag,,sizeof flag);
while(!S.empty()) S.pop();
Block=;
} void addEdge(int x,int y)
{
G[x].push_back(y);
FG[y].push_back(x);
} void dfs1(int now)
{
flag[now]=;
for(int i=; i<G[now].size(); i++)
if(!flag[G[now][i]])
dfs1(G[now][i]);
S.push(now);
} void dfs2(int now)
{
Belong[now]=Block;
for(int i=; i<FG[now].size(); i++)
if(!Belong[FG[now][i]])
dfs2(FG[now][i]);
} bool judge()
{
for(int i=; i<*N; i++) if(!flag[i]) dfs1(i);
while(!S.empty())
{
int Top=S.top();
S.pop();
if(!Belong[Top])
{
Block++;
dfs2(Top);
}
}
for(int i=; i<*N; i++)
if(Belong[i]==Belong[i+*N])
return ;
return ;
} void read()
{
int A,B,C;
for(int i=;i<N;i++)
{
addEdge(*i+,*i++*N);
addEdge(*i+,*i++*N); addEdge(*i+,*i++*N);
addEdge(*i+,*i++*N); addEdge(*i+,*i++*N);
addEdge(*i+,*i++*N);
}
for(int i=;i<N;i++)
{
scanf("%d",&X[i]); X[i]--;
if(X[i]==)
{
addEdge(*i++*N,*i+);
addEdge(*i++*N,*i+);
}
else if(X[i]==)
{
addEdge(*i++*N,*i+);
addEdge(*i++*N,*i+);
}
else if(X[i]==)
{
addEdge(*i++*N,*i+);
addEdge(*i++*N,*i+);
}
}
for(int i=;i<=M;i++)
{
scanf("%d%d%d",&A,&B,&C);
A--,B--;
if(C==)
{
addEdge(*A+,*B+);
addEdge(*A+,*B+);
addEdge(*A+,*B+); addEdge(*B+,*A+);
addEdge(*B+,*A+);
addEdge(*B+,*A+);
} else if(C==)
{
addEdge(*A+,*B++*N);
addEdge(*A+,*B++*N);
addEdge(*A+,*B++*N); addEdge(*B+,*A++*N);
addEdge(*B+,*A++*N);
addEdge(*B+,*A++*N);
}
}
} int main()
{
scanf("%d",&T);
for(int Case=;Case<=T;Case++)
{
scanf("%d%d",&N,&M);
init();
read();
printf("Case #%d: ",Case);
if(judge()) printf("yes\n");
else printf("no\n");
}
return ;
}
HDU 4115 Eliminate the Conflict的更多相关文章
- HDU 4115 Eliminate the Conflict(2-sat)
HDU 4115 Eliminate the Conflict pid=4115">题目链接 题意:Alice和Bob这对狗男女在玩剪刀石头布.已知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 ...
- 图论--2-SAT--HDU/HDOJ 4115 Eliminate the Conflict
Problem Description Conflicts are everywhere in the world, from the young to the elderly, from famil ...
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- hdu4115 Eliminate the Conflict
Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 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轮的策略不一 ...
- HDU-4115 Eliminate the Conflict 2sat
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4115 题意:Alice和Bob玩猜拳游戏,Alice知道Bob每次会出什么,为了游戏公平,Bob对Al ...
- HDU 4041 Eliminate Witches! --模拟
题意: 给一个字符串,表示一颗树,要求你把它整理出来,节点从1开始编号,还要输出树边. 解法: 模拟即可.因为由括号,所以可以递归地求,用map存对应关系,np存ind->name的映射,每进入 ...
随机推荐
- STM32F207 两路ADC连续转换及GPIO模拟I2C给MT9V024初始化参数
1.为了更好的方便调试,串口必须要有的,主要打印一些信息,当前时钟.转换后的电压值和I2C读出的数据. 2.通过GPIO 模拟I2C对镁光的MT9V024进行参数初始化.之前用我以前公司SP0A19芯 ...
- python datetime时间差
import datetime import time d1 = datetime.datetime(2005, 2, 16) d2 = datetime.datetime(2004, 12, 31) ...
- Git 操作标签的一些命令
如果标签打错了,也是可以删除: $ git tag -d v0.1Deleted tag 'v0.1' (was d96a49b) 如果要推送某个标签到远程,使用git push orign tagn ...
- PHP常用的预定义常量
<?php echo 'PHP常用的预定义常量'.'<br><br>'; echo '当前php的版本为(PHP_VERSION):'.PHP_VERSION.'< ...
- Linux(centos5.0+)unison+inotify-tools触发式双向自动同步
192.168.1.11是server1, 192.168.1.22是server2. [1]安装inotify-tools 各大linux发行版本都有inotify-tools软件包,建议通过y ...
- 数论+dp Codeforces Beta Round #2 B
http://codeforces.com/contest/2/problem/B 题目大意:给你一个n*n的矩形,问从(1,1)出发到(n,n),把图中经过的所有的数字都乘在一起,最后这个数字有多少 ...
- 设置UITabBarController上ImageInsets后点击不断缩小
最近遇到了这样一个情况,客户要求做出这种效果的UITabBarController. 通过各种查询,得出UITabBarController有这样一个属性,是设置它的图片距上下左右距离的属性: nav ...
- CSS中如何把Span标签设置为固定宽度
一.形如<span>ABC</span>独立行设置SPAN为固定宽度方法如下: span {width:60px; text-align:center; display:blo ...
- Drivers Dissatisfaction
Drivers Dissatisfaction time limit per test 4 seconds memory limit per test 256 megabytes input stan ...
- DISUBSTR - Distinct Substrings
DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its dist ...