S - Cyclic Components (并查集的理解)
Description
You are given an undirected graph consisting of n vertices and m edges. Your task is to find the number of connected components which are cycles.
Here are some definitions of graph theory.
An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex a is connected with a vertex b, a vertex b is also connected with a vertex a). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.
Two vertices u and v belong to the same connected component if and only if there is at least one path along edges connecting u and v.
A connected component is a cycle if and only if its vertices can be reordered in such a way that:
- the first vertex is connected with the second vertex by an edge,
- the second vertex is connected with the third vertex by an edge,
- ...
- the last vertex is connected with the first vertex by an edge,
- all the described edges of a cycle are distinct.
A cycle doesn't contain any other edges except described above. By definition any cycle contains three or more vertices.
There are 6 connected components, 2 of them are cycles: [7,10,16] and [5,11,9,15].

Input
The first line contains two integer numbers n and m (1≤n≤$2⋅10^5$, 0≤m≤$2⋅10^5$) — number of vertices and edges.
The following m lines contains edges: edge i is given as a pair of vertices vi, ui (1≤vi,ui≤n, ui≠vi). There is no multiple edges in the given graph, i.e. for each pair (vi,ui) there no other pairs (vi,ui) and (ui,vi) in the list of edges.
Output
Print one integer — the number of connected components which are also cycles.
Input
5 4
1 2
3 4
5 4
3 5
Output
1
Input
17 15
1 8
1 12
5 11
11 9
9 15
15 5
4 13
3 13
4 3
10 16
7 10
16 7
14 3
14 4
17 6
Output
2
Note
In the first example only component [3,4,5] is also a cycle.
The illustration above corresponds to the second example.
解题思路:并查集的运用。判断单环的条件为判断每个集合(连通分量,同一个祖先节点)中所有点的度数是否都为2,并且该集合中元素的个数至少为3个,满足这两个条件才可构成单环。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int n,m,a,b,c,cnt,fa[maxn],Deg[maxn];vector<int> vec[maxn];
void init(){//初始化
for(int i=;i<=n;++i)fa[i]=i;
}
int findt(int x){
int per=x,tmp;
while(fa[per]!=per)per=fa[per];
while(x!=per){tmp=fa[x];fa[x]=per;x=tmp;}//路径压缩
return x;
}
void unite(int x,int y){
x=findt(x),y=findt(y);
if(x!=y)fa[x]=y;
}
int main(){
cin>>n>>m;
init();cnt=;
memset(Deg,,sizeof(Deg));
for(int i=;i<=n;++i)vec[i].clear();//清空
while(m--){
cin>>a>>b;
unite(a,b);
Deg[a]++;Deg[b]++;//每个顶点的度数加1
}
for(int i=;i<=n;++i)//把同一个祖先所有的节点放在一个邻接表中
vec[findt(i)].push_back(i);
for(int i=;i<=n;++i){
if(vec[i].size()>){//构成单环的点的个数至少为3个
bool flag=false;
for(size_t j=;j<vec[i].size();++j)
if(Deg[vec[i][j]]!=){flag=true;break;}//如果度数不为2的,直接退出
if(!flag)cnt++;//如果是单环,计数器就加1
}
}
cout<<cnt<<endl;
return ;
}
S - Cyclic Components (并查集的理解)的更多相关文章
- CF-292D Connected Components 并查集 好题
D. Connected Components 题意 现在有n个点,m条编号为1-m的无向边,给出k个询问,每个询问给出区间[l,r],让输出删除标号为l-r的边后还有几个连通块? 思路 去除编号为[ ...
- [CF1303F] Number of Components - 并查集,时间倒流
有一个 \(n \times m\) 矩阵,初态下全是 \(0\). 如果两个相邻元素(四连通)相等,我们就说它们是连通的,且这种关系可以传递. 有 \(q\) 次操作,每次指定一个位置 \((x_i ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- Find them, Catch them(POJ 1703 关系并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38668 Accepted: ...
- POJ-1182 食物链(并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 75814 Accepted: 22528 Description ...
- 洛谷 P1525 【关押罪犯】种类并查集
题解 P1525 [关押罪犯]:种类并查集 前言: 在数据结构并查集中,种类并查集属于扩展域并查集一类. 比较典型的题目就是:食物链(比本题难一些,有三个种类存在) 首先讲一下本题的贪心,这个是必须要 ...
- HDU 3047 带权并查集 入门题
Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...
- ZR并查集专题
ZR并查集专题 并查集,作为一个基础算法,对于初学者来说,下面的代码是维护连通性的利器 return fa[x] == x ? x : fa[x] = getf(fa[x]); 所以,但是这对并查集的 ...
随机推荐
- 莫(meng)比(bi)乌斯反演--BZOJ2301: [HAOI2011]Problem b
n<=50000个询问,每次问a<=x<=b,c<=y<=d中有多少gcd(x,y)=K的(x,y).a,b,c,d,K<=50000. 这大概是入门题辣..这里记 ...
- Linux下汇编语言学习笔记57 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- java STW stop the world 哈哈就是卡住了
java STW stop the world 哈哈就是卡住了 学习了:http://www.jb51.net/article/125400.htm
- Django打造大型企业官网(三)
四.前端首页 4.1.导航条实现 (1)templates/new/index.html <!DOCTYPE html> <html lang="en"> ...
- 【Swift】学习笔记(二)——基本运算符
运算符是编程中用得最多的,其包含一元,二元和三元 三种运算符.swift也和其他编程语言一样基本就那些,以下总结一下,也有它特有的运算符.比方区间运算符 1.一元运算符 = 赋值运算符,用得最多的 ...
- Codeforces Round #337 (Div. 2) 610B Vika and Squares(脑洞)
B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- tf.image.resize_bilinear 图像缩放,双线性插值-图像中心对齐
http://www.cnblogs.com/yssongest/p/5303151.html 双线性插值算法及需要注意事项 input = tf.placeholder(tf.float32, sh ...
- Linux进程间通信 共享内存+信号量+简单样例
每个进程都有着自己独立的地址空间,比方程序之前申请了一块内存.当调用fork函数之后.父进程和子进程所使用的是不同的内存. 因此进程间的通信,不像线程间通信那么简单.可是共享内存编程接口能够让一个进程 ...
- 网络学习笔记:TCP/IP连网和Internet
1.网关 由硬件和软件组成,实现不同网段间的数据传送. 常用路由器充当网关. 网关通常维护一份路由表,但只有少量的编址信息.它用这些信息把数据转发到知道更多信息的网关. 组成互联网骨干的网关称为核心网 ...
- javascript里的prototype
在javascript中,prototype是函数的一个固有属性,其他对象,比如字符串什么的,并没有这个属性. 这个属性做什么用呢? 1.用于该函数的所有实例进行共享 比如,共同的属性,共同的方法.类 ...