bzoj1116 [POI2008]CLO——并查集找环
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1116
分析性质,只要有环,那么给环定一下向就满足了条件;
环上点的其他边可以指向外面,所以两个连通块合并时只要一个有环,那么整个连通块就都可以了。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const maxn=1e5+,maxm=2e5+;
int n,m,fa[maxn];
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
bool b[maxn];
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)fa[i]=i;
for(int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(find(x)==find(y))b[find(x)]=;
else
{
b[find(y)]|=b[find(x)];
fa[find(x)]=find(y);
}
}
for(int i=;i<=n;i++)
if(!b[find(i)])
{
printf("NIE\n"); return ;
}
printf("TAK\n");
return ;
}
bzoj1116 [POI2008]CLO——并查集找环的更多相关文章
- BZOJ1116:[POI2008]CLO(并查集)
Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 你要把其中一些road变成单向边使得:每个t ...
- 【BZOJ1116】[POI2008]CLO 并查集
[BZOJ1116][POI2008]CLO Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. ...
- Codeforces Round #346 (Div. 2) E题 并查集找环
E. New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to it ...
- Codeforces 859E Desk Disorder 并查集找环,乘法原理
题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...
- BZOJ 1116: [POI2008]CLO 并查集
成立时当且仅当每个联通块都有环存在.一个连通块若有m个点,则必有多于m条有向边,可用并查集来维护. #include<cstdio> #include<iostream> #d ...
- cf246 ENew Reform (并查集找环)
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each ...
- [BZOJ1116]CLO[并查集]
看了样例突然发现= =无向边不会增加入度. 然后发现是环套环. 一个环所有点入度都为2. 最后的图无视所有无向边的话大概是这样的(将就一下 然后就可以并查集维护一下联通性... 当x , y属于一个联 ...
- BZOJ1116: [POI2008]CLO
1116: [POI2008]CLO Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 565 Solved: 303[Submit][Status] ...
- poj 3310(并查集判环,图的连通性,树上最长直径路径标记)
题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...
随机推荐
- 轻松理解 Android Binder,只需要读这一篇
在 Android 系统中,Binder 起着非常重要的作用,它是整个系统 IPC 的基石.网上已经有很多文章讲述 Binder 的原理,有的讲的比较浅显,没有触及到关键,有的讲的太过于深入底层,难以 ...
- CentOS / RHEL 7 : Chrony V/s NTP (Differences Between ntpd and chronyd)
CentOS / RHEL 7 : Chrony V/s NTP (Differences Between ntpd and chronyd) Chosing between Chrony and N ...
- linu学习第二天:文件系统相关操作
1 ---第二天笔记--- 2 查看操作系统版本:cat /etc/redhat-release, /etc/os-release 3 命令:lsb_release 4 查看内存 和 swap分区:f ...
- Problem 56
Problem 56 https://projecteuler.net/problem=56 Powerful digit sum A googol (10100) is a massive numb ...
- GlobalSign 域名型 SSL 证书
GlobalSign 域名型 SSL 证书,支持通配符型,只验证域名所有权,属于DV 域名验证级SSL证书,无须递交书面审查资料,网上申请便捷有效率.提供40位/56位/128位,最高256位自适 ...
- LINUX 查看硬件配置命令
系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ...
- CDOJ 889 Battle for Silver
Battle for Silver Time Limit: 2999/999MS (Java/Others) Memory Limit: 65432/65432KB (Java/Others) ...
- CF410div2 A. Mike and palindrome
/* CF410div2 A. Mike and palindrome http://codeforces.com/contest/798/problem/A 水题 */ #include <c ...
- poj3233 题解 矩阵乘法 矩阵快速幂
题意:求S = A + A2 + A3 + … + Ak.(mod m) 这道题很明显可以用矩阵乘法,但是这道题的矩阵是分块矩阵, 分块矩阵概念如下:当一个矩阵A中的单位元素aij不是一个数值而是一个 ...
- MapReduce Shuffle优化方向
Shuffle过程介绍可以查看该博客:http://langyu.iteye.com/blog/992916 优化方向: 压缩:对数据进行压缩,减少写读数据量: 减少不必要的排序:并不是所有类型的Re ...