You are given an undirected graph consisting of nn vertices and mm 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 aa is connected with a vertex bb, a vertex bb is also connected with a vertex aa). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.

Two vertices uu and vv belong to the same connected component if and only if there is at least one path along edges connecting uu and vv.

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 66 connected components, 22 of them are cycles: [7,10,16][7,10,16] and [5,11,9,15][5,11,9,15].

Input

The first line contains two integer numbers nn and mm (1≤n≤2⋅1051≤n≤2⋅105, 0≤m≤2⋅1050≤m≤2⋅105) — number of vertices and edges.

The following mm lines contains edges: edge ii is given as a pair of vertices vivi, uiui (1≤vi,ui≤n1≤vi,ui≤n, ui≠viui≠vi). There is no multiple edges in the given graph, i.e. for each pair (vi,uivi,ui) there no other pairs (vi,uivi,ui) and (ui,viui,vi) in the list of edges.

Output

Print one integer — the number of connected components which are also cycles.

Examples
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][3,4,5] is also a cycle.

The illustration above corresponds to the second example.

题意:

  让你求回路的个数,而且这个回路是没有杂边的单环。

思路

  如果有这样的回路,那么每一个节点的度数一定为2。用dfs跑一遍,如果在跑的过程中,所有的点度数均为2,那么它一定就是我们要找的环。

  这里我使用了一种新的邻接表,使用vector,这种方式没有办法存储边的长度,但是可以很直接的看出点的度数。如果要用vector来储存权值的话,那么再开一个vector,依次记录就行了

#include<iostream>
#include<vector>
using namespace std;
int book[];
int g;
vector<int>a[]; void dfs(int x)
{
book[x]=;
if(a[x].size()!=){g=;} for(int i:a[x]){
if(!book[i]){dfs(i);}
}
} int main()
{
int n,m;
cin>>n>>m;
int x,y;
for(int i=;i<=m;i++){
cin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
int ans=;
for(int i=;i<=n;i++){
g=;
if(!book[i]){
dfs(i);
if(!g){ans++;}
}
}
cout<<ans<<endl;
}

以上思路来自于大神代码:

Codeforce Div-3 E.Cyclic Components的更多相关文章

  1. Codeforces Round #479 (Div. 3) E. Cyclic Components (思维,DFS)

    题意:给你\(n\)个顶点和\(m\)条边,问它们有多少个单环(无杂环),例如图中第二个就是一个杂环. 题解:不难发现,如果某几个点能够构成单环,那么每个点一定只能连两条边.所以我们先构建邻接表,然后 ...

  2. CF 977E Cyclic Components

    E. Cyclic Components time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. Cyclic Components CodeForces - 977E(DFS)

    Cyclic Components CodeForces - 977E You are given an undirected graph consisting of nn vertices and  ...

  4. 【codeforces div3】【E. Cyclic Components】

    E. Cyclic Components time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. E. Cyclic Components (DFS)(Codeforces Round #479 (Div. 3))

    #include <bits/stdc++.h> using namespace std; *1e5+; vector<int>p[maxn]; vector<int&g ...

  6. Codeforce 977E Cyclic Components

    dfs判断图的连通块数量~ #include<cstdio> #include<algorithm> #include<vector> #include<cs ...

  7. S - Cyclic Components (并查集的理解)

    Description You are given an undirected graph consisting of nn vertices and mm edges. Your task is t ...

  8. Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components? 图论

    E. Connected Components? You are given an undirected graph consisting of n vertices and edges. Inste ...

  9. codeforce div 377

    #include <bits/stdc++.h> using namespace std; #define pb push_back #define lb lower_bound #def ...

随机推荐

  1. PairProject 总结

    结对编程人员:张迎春,赵梓皓.下面是我们一起编程的照片. 结对编程的优点: 首先,结对编程的目的是为了减少编程的错误,在编程的时候,大家一起检查错误,一起分析有没有更加合理的编写方法,所以这是结对编程 ...

  2. 11.13 Daily Scrum

    今天在实现餐厅列表时,原来使用的百度地图poi搜索接口无法返回餐厅的具体信息. 经过一番周折,找到了一个返回餐厅url的接口.我们调整了一下实现,在点击餐厅列表的某一项点击直接跳到和该餐厅信息有关的网 ...

  3. 【个人博客作业Week7】软件工程团队项目一轮迭代感想与反思

    (发布晚原因:发到团队博客了 一.关于银弹 在佛瑞德·布鲁克斯于1986年发布的<没有银弹:软件工程的本质性与附属性工作>这篇软件工程的经典论文中,作者向我们讲述了软件工程没有银弹这样的理 ...

  4. "Linux内核分析"第六周实验报告

    张文俊 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 1.进程的描述 ...

  5. 实践——ELF文件格式分析

    一.分析文件头 1. 段入口类型定义(/usr/include/elf.h)下面产生的hello是32位的 使用命令#Hexdump –x ELF_1.o 第一行: 前4字节,蓝色部分,是一个魔数,表 ...

  6. jQuery(五)

    wrap <script> //wrap:包装 //wrapAll:整体包装 //wrapInner:内部包装 //unwrap:删除包装(删除父级,不包括body) $(function ...

  7. Java 编码规范 StandardCharsets.UTF_8 三个方法 toString() name() displayName(),到底用哪个方法更合适?

    想用StandardCharsets.UTF_8 返回"UTF-8"这个字符,测试一下,三个方法toString() name() displayName(),均能返回" ...

  8. ceph S3客户端操作--s3cmd

    S3 client 访问ceph rgw 安装: yum install s3cmd 验证安装是否成功: $s3cmd --version s3cmd version 1.5.2 #表示安装成功 在c ...

  9. Java监听器Listener的使用详解

    监听器用于监听Web应用中某些对象的创建.销毁.增加,修改,删除等动作的发生,然后作出相应的响应处理.当监听范围的对象的状态发生变化的时候,服务器自动调用监听器对象中的方法.常用于统计网站在线人数.系 ...

  10. hdu 6393 Traffic Network in Numazu (树链剖分+线段树 基环树)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6393 思路:n个点,n条边,也就是基环树..因为只有一个环,我们可以把这个环断开,建一个新的点n+1与之相 ...