Let's go home

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

Total Submission(s): 1616    Accepted Submission(s): 661

Problem Description
小时候,乡愁是一枚小小的邮票,我在这头,母亲在那头。

—— 余光中



集训是辛苦的。道路是坎坷的,歇息还是必须的。经过一段时间的训练,lcy决定让大家回家放松一下,可是训练还是得照常进行,lcy想出了例如以下回家规定,每个队(三人一队)或者队长留下或者其余两名队员同一时候留下;每一对队员,假设队员A留下,则队员B必须回家歇息下,或者B留下,A回家。因为今年集训队人数突破往年同期最高记录,管理难度相当大。lcy也不知道自己的决定是否可行,所以这个难题就交给你了,呵呵,优点嘛~。免费**漂流一日。

 
Input
第一行有两个整数,T和M,1<=T<=1000表示队伍数。1<=M<=5000表示对数。

接下来有T行,每行三个整数,表示一个队的队员编号,第一个队员就是该队队长。

然后有M行,每行两个整数,表示一对队员的编号。

每一个队员仅仅属于一个队。

队员编号从0開始。

 
Output
可行输出yes,否则输出no,以EOF为结束。
 
Sample Input
1 2
0 1 2
0 1
1 2 2 4
0 1 2
3 4 5
0 3
0 4
1 3
1 4
 
Sample Output
yes
no
 
Author
威士忌
 
Source
解题思路:
2-SAT就是两者有冲突就连边。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#define LL long long
using namespace std;
const int MAXN = 20010;
vector<int>G[MAXN];
int pre[MAXN], lowlink[MAXN], sccno[MAXN], dfs_clock, scc_cnt;
stack<int> s;
void dfs(int u)
{
pre[u] = lowlink[u] = ++dfs_clock;
s.push(u); int sz = G[u].size();
for(int i=0;i<sz;i++)
{
int v = G[u][i];
if(!pre[v])
{
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}
else if(!sccno[v])
{
lowlink[u] = min(lowlink[u], pre[v]);
}
}
if(lowlink[u] == pre[u])
{
scc_cnt++;
for(;;)
{
int x = s.top(); s.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
}
void find_scc(int n)
{
dfs_clock = scc_cnt = 0;
memset(sccno, 0, sizeof(sccno));
memset(pre, 0, sizeof(pre));
for(int i=1;i<=n;i++) if(!pre[i])
dfs(i);
}
int N, M, T;
int main()
{
while(scanf("%d%d", &T, &M)!=EOF)
{
int x, y, z;
N = 3 * T;
for(int i=0;i<=2*N;i++) G[i].clear();
for(int i=1;i<=T;i++)
{
scanf("%d%d%d", &x, &y, &z);
x++; y++; z++;
G[x+N].push_back(y);
G[x+N].push_back(z);
G[y+N].push_back(x);
G[z+N].push_back(x);
}
for(int i=1;i<=M;i++)
{
scanf("%d%d", &x, &y);
x++; y++;
G[x].push_back(y + N);
G[y].push_back(x + N);
}
find_scc(2 * N);
int flag = 1;
for(int i=1;i<=N;i++)
{
if(sccno[i] == sccno[i + N])
{
flag = 0;
break;
}
}
if(flag) puts("yes");
else puts("no");
}
return 0;
}

HDU 1824 Let&#39;s go home (2-SAT判定)的更多相关文章

  1. HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT

    一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...

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

  3. hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组)

    pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...

  4. HDU 3966 Aragorn&#39;s Story(树链剖分)

    HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...

  5. hdu 5282 Senior&#39;s String 两次dp

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282 Senior's String Time Limit: 2000/1000 MS (Java/Oth ...

  6. HDU 3177 Crixalis&#39;s Equipment(贪婪)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=3177 Problem Description Crixalis - Sand King used t ...

  7. HDU - 5186 - zhx&#39;s submissions (精密塔尔苏斯)

    zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  8. HDU 2120 Ice_cream&#39;s world I(并检查集合)

    职务地址:HDU 2120 这题尽管字数不多,但就是看不懂. . 意思是求最多有多少个被墙围起来的区域.显然就是求环的个数.然后用并查集求环个数就能够了. 代码例如以下: #include <i ...

  9. HDU 3729 I&#39;m Telling the Truth(二部图最大匹配+结果输出)

    职务地址:HDU 3729 二分图最大匹配+按字典序输出结果. 仅仅要从数字大的開始匹配就能够保证字典序最大了.群里有人问. . 就顺手写了这题. . 代码例如以下: #include <ios ...

随机推荐

  1. C# 如何正确删除List中的item

    参考博客 https://blog.csdn.net/Le_Sam/article/details/75633737 https://www.cnblogs.com/hedianzhan/p/9130 ...

  2. poj3710 Christmas Game

    题目描述 题解: 树上删边. 对于奇数长度的环,可以看做一条边. 对于偶数长度的环,可以看做什么都没有. 没有特别好的解释…… 代码: #include<cstdio> #include& ...

  3. 【MySQL】索引和锁

    前言 本文摘自数据库两大神器[索引和锁] 声明:如果没有说明具体的数据库和存储引擎,默认指的是MySQL中的InnoDB存储引擎 索引 在之前,我对索引有以下的认知: 索引可以加快数据库的检索速度 表 ...

  4. python之 集合 学习笔记

    """ 集合内的元素是无序的,集合内的元素必须是可哈希的集合内元素的唯一的,不存在重复列表和字典不能存在集合里面,因为列表字典可变 可哈希集合也是不可哈希的 unhash ...

  5. LeetCode(19) Remove Nth Node From End of List

    题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...

  6. 刁肥宅详解中缀表达式求值问题:C++实现顺序/链栈解决

    1. 表达式的种类 如何将表达式翻译成能够正确求值的指令序列,是语言处理程序要解决的基本问题,作为栈的应用事例,下面介绍表达式的求值过程. 任何一个表达式都是由操作数(亦称运算对象).操作符(亦称运算 ...

  7. Python+selenium登录测试

    我们以登录新浪微博为案例来讲解,首先进入登录页面,输入用户名和密码,点击登录按钮,并且获得用户信息以验证是否登录成功. Web地址:https://login.sina.com.cn/signup/s ...

  8. HDU-1858-Max Partial Value I,有坑点,不难;

    Max Partial Value I Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Othe ...

  9. .NET下 JSON 的一些常用操作

    1.JSON的序列化和反序列化 Newtonsoft.Json dll 下载地址http://json.codeplex.com/ using System; using System.Collect ...

  10. [luoguP3694] 邦邦的大合唱站队/签到题(状压DP)

    传送门 来自kkk的题解: 70分做法:枚举每个学校顺序,暴力. 100分:状压dp.从队列头到尾DP, 状态:f[i]表示i状态下最小的出列(不一致)的个数. 比如f[1101]表示从头到位为1/3 ...