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 (并查集的理解)的更多相关文章

  1. CF-292D Connected Components 并查集 好题

    D. Connected Components 题意 现在有n个点,m条编号为1-m的无向边,给出k个询问,每个询问给出区间[l,r],让输出删除标号为l-r的边后还有几个连通块? 思路 去除编号为[ ...

  2. [CF1303F] Number of Components - 并查集,时间倒流

    有一个 \(n \times m\) 矩阵,初态下全是 \(0\). 如果两个相邻元素(四连通)相等,我们就说它们是连通的,且这种关系可以传递. 有 \(q\) 次操作,每次指定一个位置 \((x_i ...

  3. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  4. 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条臭 ...

  5. Find them, Catch them(POJ 1703 关系并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38668   Accepted: ...

  6. POJ-1182 食物链(并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 75814   Accepted: 22528 Description ...

  7. 洛谷 P1525 【关押罪犯】种类并查集

    题解 P1525 [关押罪犯]:种类并查集 前言: 在数据结构并查集中,种类并查集属于扩展域并查集一类. 比较典型的题目就是:食物链(比本题难一些,有三个种类存在) 首先讲一下本题的贪心,这个是必须要 ...

  8. HDU 3047 带权并查集 入门题

    Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...

  9. ZR并查集专题

    ZR并查集专题 并查集,作为一个基础算法,对于初学者来说,下面的代码是维护连通性的利器 return fa[x] == x ? x : fa[x] = getf(fa[x]); 所以,但是这对并查集的 ...

随机推荐

  1. Codeforces704B. Ant Man

    n<=5000个数轴上的点,有属性x,a,b,c,d,从i跳到j的代价如下: 问从s跳到t的最小代价. 方法?:先构造s->t链,然后依次插入其他点,每次选个最佳的位置.过了这题,正确性不 ...

  2. CURL不可以读写文件

    最近在学ES(elastic search),参考http://www.learnes.net/里面翻译的官方权威指南(后面发现官网已经推出了中文版文档了).里面有的例子把访问ES的命令做了简化如下: ...

  3. 基于jQuery的图片加载loading效果插件

    基于jQuery的图片加载loading效果插件 图片loading的效果是网页中比较常见的,尤其是对大图片,loading效果让用户能够明白图片加载的过程. 实现思路也是比较简单的: $.fn.Lo ...

  4. BitMap算法 .net实现 用于去重并且排序,适用于大型权限管理 ,大数据去重排序

    BitMap利用byte特性 针对排序+去重  最佳实践: 100万条数据的排序+去重用时200毫秒左右 static void Main(string[] args) { ]; /*alias*/ ...

  5. [bzoj1978][BeiJing2010]取数游戏 game_动态规划_质因数分解

    取数游戏 game bzoj-1978 BeiJing-2010 题目大意:给定一个$n$个数的$a$序列,要求取出$k$个数.假设目前取出的数是$a_j$,那么下次取出的$a_k$必须保证:$j&l ...

  6. Java电商项目-5.内容管理cms系统

    目录 实现加载内容分类树功能 实现内容分类动态添加 删除内容分类节点 实现内容分类节点的分页显示 实现广告内容的添加 实现广告内容删除 实现广告内容编辑 到Github获取源码请点击此处 实现加载内容 ...

  7. Ubuntu 16.04安装网络流量监控工具Netspeed(附带10款最佳的指示器工具)

    安装: sudo add-apt-repository ppa:ferramroberto/linuxfreedomlucid sudo apt-get update sudo apt-get ins ...

  8. USB2.0的最高传输速率

    USB2.0除了拥有USB1.1中规定的1.5Mbps和12Mbps两个传输模式以外,还增加了480Mbps高速数据传输模式(注:第二版USB2.0的传输速率将达800Mbps,最高理想值1600Mb ...

  9. cn_windows_10_multiple_editions_version_1607_updated_jul_2016_x64

    ed2k://|file|cn_windows_10_multiple_editions_version_1607_updated_jul_2016_x64_dvd_9056935.iso|43471 ...

  10. 在DevExpress GridControl的一列中显示图片

    作者:jiankunking 出处:http://blog.csdn.net/jiankunking 近期做项目的时候用到了将GridControl中一列设置为PictureEdit类型,然后通过这一 ...