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),然后强连通缩 ...
随机推荐
- 全排列dfs算法
如下 #include <iostream> using namespace std; #define MAX 10 #define _CRT_SECURE_NO_WARNINGS int ...
- Merge Into 用法
从一个数据库的一张表同步数据到另外一个数据库的一张表,同步的数据不是insert就是update. 一般做法是先判断当前数据在另外一张表存不存在,存在则更新,不存在则插入.需要一次查询判断:exist ...
- 转:CSV Data Set Config 中文乱码问题
从csv读取中文一直乱码. CSV Data Set Config的File encoding为GB2312,对应参数化文件编码也为GB2312,但读取出变量值一直为乱码,后发现是Allow quot ...
- VoIP的话音质量测量方法
严重的呼叫质量和性能管理问题会影响VoIP (Voice over IP)系统的运作.网络管理员等人需要理解基本的呼叫质量测量技术才能很好地监测.管理和诊断在VoIP中出现的这些问题.本文介绍了常用的 ...
- 编码规范系列(二):Eclipse Checkstyle配置
http://chenzhou123520.iteye.com/blog/1627618 上一篇介绍了<编码规范系列(一):Eclipse Code Templates设置>,这篇主要介绍 ...
- 新随笔ps泡泡制作
http://jingyan.baidu.com/article/4d58d5413568a79dd4e9c016.html
- hdu_1072_Nightmare(BFS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 题意:给你一个地图,让你在炸弹爆之前找到出口,最初炸弹设定为6,每走一格需要1,中途有地方能让炸 ...
- SharePoint 2010 Modal Dialog
SharePoint 2010 Modal Dialog Tweet Modal dialog play very important role to improve the user exper ...
- Gridview导出成Excel
在aspx里面加上 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AAAAAA ...
- SB淘宝api的奇葩问题! 一则服务器无法访问淘宝api
<?xml version="1.0" encoding="utf-8" ?><error_response><code>3 ...