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)
Total Submission(s): 1616 Accepted Submission(s): 661
—— 余光中
集训是辛苦的。道路是坎坷的,歇息还是必须的。经过一段时间的训练,lcy决定让大家回家放松一下,可是训练还是得照常进行,lcy想出了例如以下回家规定,每个队(三人一队)或者队长留下或者其余两名队员同一时候留下;每一对队员,假设队员A留下,则队员B必须回家歇息下,或者B留下,A回家。因为今年集训队人数突破往年同期最高记录,管理难度相当大。lcy也不知道自己的决定是否可行,所以这个难题就交给你了,呵呵,优点嘛~。免费**漂流一日。
接下来有T行,每行三个整数,表示一个队的队员编号,第一个队员就是该队队长。
然后有M行,每行两个整数,表示一对队员的编号。
每一个队员仅仅属于一个队。
队员编号从0開始。
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
yes
no
#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's go home (2-SAT判定)的更多相关文章
- 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 3966 Aragorn's Story(树链剖分+树状数组)
pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...
- HDU 3966 Aragorn's Story(树链剖分)
HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...
- hdu 5282 Senior's String 两次dp
题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282 Senior's String Time Limit: 2000/1000 MS (Java/Oth ...
- HDU 3177 Crixalis's Equipment(贪婪)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=3177 Problem Description Crixalis - Sand King used t ...
- HDU - 5186 - zhx's submissions (精密塔尔苏斯)
zhx's submissions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 2120 Ice_cream's world I(并检查集合)
职务地址:HDU 2120 这题尽管字数不多,但就是看不懂. . 意思是求最多有多少个被墙围起来的区域.显然就是求环的个数.然后用并查集求环个数就能够了. 代码例如以下: #include <i ...
- HDU 3729 I'm Telling the Truth(二部图最大匹配+结果输出)
职务地址:HDU 3729 二分图最大匹配+按字典序输出结果. 仅仅要从数字大的開始匹配就能够保证字典序最大了.群里有人问. . 就顺手写了这题. . 代码例如以下: #include <ios ...
随机推荐
- vim全选复制
网上一堆答案全是ggyG,根本不行, 正确答案应该是 gg"*yG 或者 gg"*+yG 下面是在stack overflow 上找到的答案,亲测有效,在此记录下 stackove ...
- oracle 存储过程,存储函数,包,
http://heisetoufa.iteye.com/blog/366957 认识存储过程和函数 存储过程和函数也是一种PL/SQL块,是存入数据库的PL/SQL块.但存储过程和函数不同于已经介绍过 ...
- [LOJ] 分块九题 3
https://loj.ac/problem/6279 区间修改,区间查询前驱. TLE无数,我觉得这代码最精髓的就是block=1000. 谜一样的1000. 两个启示: 块内可以维护数据结构,比如 ...
- 用python的Requests库模拟http请求
一.先了解几个重要的http请求头或响应头信息 Request Headers: Host: 描述请求将被发送的目的地,包括,且仅仅包括域名和端口号. Origin: 说明请求从哪里发起的,包括,且仅 ...
- (1) openssl基础概念
1.1 背景知识 对称加密 :加密解密使用同一密钥,加解密速度快.随着人数增多,密钥数量急增n(n-1)/2. 非对称加密 :使用公私钥配对加解密,速度慢.公钥是从私钥中提取出来的,一般拿对方 ...
- 我的java web之路(JSP基本语法)
1.JSP注释 1.1输出注释 语法格式 <!--comment [<%= expression %>] --> <body> This is my JSP pa ...
- CentOS 7下安装Composer + Laravel
1.wget https://dl.laravel-china.org/composer.phar -O /usr/local/bin/composer chmod a+x /usr/local/bi ...
- Linux MTD (Memory Technology Device) subsystem analysis -For Atheros char device
Linux MTD (Memory Technology Device) subsystem analysis For Atheros char device 读了Linux MTD 源代码分析 对这 ...
- HDU 6390
GuGuFishtion Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- js删除数组对象中符合条件的数据
var data = [{}, {}, {}, {Id:1}] var datawilldele = [];//2,4,5 data.forEach(function (v, i,arry) { if ...