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. hash存储结构【六】

    一.概述: 我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器.所以该类型非常适合于存储值对象的信息.如Username.Password和Ag ...

  2. cogs——555. 网络探测

    555. 网络探测 ★☆   输入文件:ping.in   输出文件:ping.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述]    当出现网络故障时,我们经常使用“p ...

  3. Ubuntu 16.04系统启动时卡在:(initramfs)

    背景: 由于不正常的关机和重启,或者突然断电导致的关机,下次起来后不能进去系统,停留在(initramfs). 解决方法: 使用如下命令修复 fsck -y /dev/sda1 说明:其中sda1为系 ...

  4. JSP中访问数据库

    在JSP中访问数据库使用的是JSTL标签,本文不按照http://wiki.jikexueyuan.com/project/jsp/database-access.html此方法进行实践,而是采用之前 ...

  5. shell脚本变量的参数

    https://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables mkdir f ...

  6. RHEL6.5上Oracle ACFS与Linux samba一起使用时遇到的bug

    RHEL上的Oracle ACFS与linux samba一起使用时遇到的bug 一.环境介绍: cat /etc/issue的结果为: Red Hat Enterprise Linux Server ...

  7. CCNP路由实验之八 路由重公布

     CCNP路由实验之八 路由重公布 在前面几个实验,已经学习了静态路由和动态路由.如今,我们要掌握怎样使它们在一个网络中融合,即路由重公布. 使用出站口作为静态路由 0 使用下一跳地址作为静态路由 ...

  8. 怎样用fiddler2捕获移动设备上的http或者https请求

    调试移动设备上的问题.看不到发送的请求和得到的响应是比較难过的,fiddler能够实现样的功能. 原理: 在PC上启动fiddler.将手持设备的网络代理改成fiddler. 这样全部的请求和响应都经 ...

  9. 使用shell分页读取600万+的MySQL数据脚本

    shell-mysql 脚本背景 因为要在Linux上.远程读取mysql的表的数据,然后做一定清洗后.把数据上传至Hadoop集群中,使用Java写吧,感觉太麻烦了.得在Win上开发好,还得打成ja ...

  10. C - The C Answer (2nd Edition) - Exercise 1-5

    /* Modify the temperature conversion program to print the table in reverse order, that is, from 300 ...