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找树上最长直径了,并且对树上最长直径 ...
随机推荐
- JS——动态添加事件和移除事件(有待补充...)
动态的添加事件:利用 attachEvent 和 addEventListener IE 支持 attachEvent: obj.attachEvent("onclick", Fo ...
- (转)分布式文件存储FastDFS(六)FastDFS多节点配置
http://blog.csdn.net/xingjiarong/article/details/50759918 前面几篇关于FastDFS的博客中介绍了如何在一台机器上搭建一个简易的FastDFS ...
- Django中 基于form的注册,基于ajax的登录
1 form.py中写register的的form组件 from django import forms class Register(forms.Form): # 注册的form username ...
- CAD在网页中得到批注信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- 删数据ORA-02292主键约束问题
通常在删除某个表A的时候,会出现这个错误.原因是另一个表B的某个字段引用了A表的某个字段作为约束(这个的另一个说法是外键). 假如引用的字段叫field,当B.field = A.field , 而你 ...
- Django - 创建多对多及增加示例
创建多对多: 方式一: 自定义关系表 备注:自定义表Host.Application,通过自定义表,将表Host和Application进行关联(通过外键方式工): 执行语句:python manag ...
- SqlServer Function
set quoted_identifier on; set ansi_nulls on; go create function [dbo].[Get_StrArrayStrOfIndex] ( @st ...
- python数字取反~
>>> a = [1,2,3,4,5,7,6,4,2,10] >>> h = len(a)//2 >>> h 5 >>> ~h ...
- hstack()与vstack()函数
ref: https://blog.csdn.net/csdn15698845876/article/details/73380803 1. hstack()函数 a,b只有一个维度:对第一个维度拼接 ...
- Shell 脚本编程 基本语法:
Shell 脚本编程语法: 注: 文章来源 http://www.cnblogs.com/yunquan/p/6821850.html 视频来源:https://www.bilibili.com/vi ...