HDU 1824 Let's go home
2-SAT,根据题意建好图,求一下强联通分量,判断一下就可以了
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std; const int maxn=; int T,M;
vector<int>G[maxn];
vector<int>FG[maxn]; int Belong[maxn],flag[maxn];
stack<int>S;
int Block; void init()
{
if(!S.empty()) S.pop();
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);
Block=;
} 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]);
} void Add(int x,int y)
{
G[x].push_back(y);
FG[y].push_back(x);
} int main()
{
while(~scanf("%d%d",&T,&M))
{
init();
for(int i=; i<T; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
Add(*T+a,b);
Add(*T+a,c);
Add(*T+b,a);
Add(*T+c,a);
}
for(int i=; i<M; i++)
{
int a,b;
scanf("%d%d",&a,&b);
Add(a,*T+b);
Add(b,*T+a);
} for(int i=; i<*T; i++) if(!flag[i]) dfs1(i);
while(!S.empty())
{
int Top=S.top();
S.pop();
if(!Belong[Top])
{
Block++;
dfs2(Top);
}
}
int ans=;
for(int i=; i<*T; i++)
{
if(Belong[i]==Belong[*T+i])
{
ans=;
break;
}
}
if(ans==) printf("yes\n");
else printf("no\n");
}
return ;
}
HDU 1824 Let's go home的更多相关文章
- HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT
一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...
- HDU 1824 Let's go home(2-SAT+Tarjan)
Let's go home Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 1824
也是一道2-sat的入门题: 不过题目描述的不清楚,看了别人的题解才知道题意: 和上面的那题差不多,一个模板: 代码: #include<cstdio> #include<stack ...
- 2-sat(tarjan算法)hdu(1824)
hdu1824 Let's go home Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 1824 2-sat问题(判断)
/* 题意:u,v,w队长,队员,队长留下两个队员可以回家,两个队员留下,队长回家 2-sat问题,把两个队员看成一个整体就变成一个简单2-sat问题了 */ #include<stdio.h& ...
- HDU 1824 Let's go home (2-SAT判定)
Let's go home Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 图论--2-SAT--HDOJ/HDU 1824 Let's go home
Problem Description 小时候,乡愁是一枚小小的邮票,我在这头,母亲在那头. -- 余光中 集训是辛苦的,道路是坎坷的,休息还是必须的. ...
- 【图论】2-sat总结
2-sat总结 2-sat问题,一般表现的形式为.每一个点有两种方式a,b,要么选a,要么选b.而且点点之间有一些约束关系.比如:u和v至少一个选a.那么这就是一个表达式.把a当成真,b当成假,那就是 ...
- hdu 3062+1824(2-sat入门)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062 思路:根据矛盾关系连边(如果a与b矛盾,则连边a'->b,b'->a),然后强连通缩 ...
随机推荐
- Prime Path(BFS)
Prime Path Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- Subordinates
Subordinates time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- php 链接 sqlserver 2005以上版本数据库
<?php /** * 数据库管理 * * @author wangaibo168@163.com * @charset utf-8 * 不支持sqlserver2005(包括)以下的版本 */ ...
- tree btn
@* <a href="javascript:void(0)" onclick="saveChecked()" ><img sr ...
- 用yum安装JDK(CentOS)
1.查看yum库中都有哪些jdk版本 [root@localhost ~]# yum search java|grep jdk 2.选择版本,进行安装 [root@localhost ~]# yum ...
- U盘启动安装Ubuntu
1.UltraISO制作USB启动盘 2.打开U盘目录下的\syslinux\syslinux.cfg, 将default vesamenu.c32注释为 # default vesamenu.c32
- 第一次使用unity3d
今天暂且做个记录,因为第一使用了unity3d,进行了很长时间的安装和调试,进行了简单的使用,能简单的在页面上面建立了一个方块和一个球. 简单了解了unity中的一些基本概念.总结一下,一个物体可以有 ...
- python 文件夹操作
常用的一些 import os for d, cd, fl in os.wald('.'): for f in fl: sufix = os.path.splitext(f)[1][1:] if su ...
- java中的静态代码块等执行顺序
http://www.cnblogs.com/naruto469/p/3608459.html public class Print { 2 3 public Print(String s){ 4 S ...
- HDU1062:Text Reverse
Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...