Cyclic Components CodeForces - 977E

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

Input

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

The following mm lines contains edges: edge ii 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 (vi,ui) in the list of edges.

Output

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

Examples
input

Copy
5 4
1 2
3 4
5 4
3 5
output

Copy
1
input

Copy
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

Copy
2
Note

In the first example only component [3,4,5] is also a cycle.

The illustration above corresponds to the second example.

分析:DFS 如果其中一个连通图的所有点的度数都为2就符合题意(搜索完某一连通图就把该连通图的所有点做标记,不再访问)

代码:

#include<bits/stdc++.h>
using namespace std;
const int N = + ;
vector<int> a[N];
int vis[N];
int flag = ;
void dfs(int cur) {
vis[cur] = ;
if(a[cur].size() != ) flag = ;
for(int i : a[cur]) {
if(!vis[i]) dfs(i);
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
int x, y;
for(int i = ; i < m; i++) {
scanf("%d%d", &x, &y);
a[x].push_back(y);
a[y].push_back(x);
}
memset(vis, , sizeof(vis));
int ans = ;
for(int i = ; i <= n; i++) {
flag = ;
if(!vis[i]) {
dfs(i);
if(flag) ans++;
}
}
printf("%d\n", ans);
return ;
}

Cyclic Components CodeForces - 977E(DFS)的更多相关文章

  1. Cyclic Components CodeForces - 977E(找简单环)

    题意: 就是找出所有环的个数, 但这个环中的每个点都必须只在一个环中 解析: 在找环的过程中 判断度数是否为2就行...emm... #include <bits/stdc++.h> us ...

  2. CF 977E Cyclic Components

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

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

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

  4. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  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. Codeforces Round #479 (Div. 3) E. Cyclic Components (思维,DFS)

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

  7. Codeforces 977E:Cyclic Components(并查集)

    题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...

  8. Codeforce 977E Cyclic Components

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

  9. Codeforce Div-3 E.Cyclic Components

    You are given an undirected graph consisting of nn vertices and mm edges. Your task is to find the n ...

随机推荐

  1. hibernate事务规范写法

    @Test public void testTx() { SessionFactory sessionFactory = null; Session session = null; Transacti ...

  2. redhat 配置yum源(配置163 yum repo)

    一般安装好redhat后,不能注册的话,不能使用系统自带的yum源.但是我们可以自己配置yum源来解决这一问题.下面介绍下redhat配置163yum源. 1) 查看版本号和系统类别: cat /et ...

  3. Windows.环境变量(设置)

    ZC: 我的示例代码(Delphi):http://www.cnblogs.com/CodeSkill/p/8341464.html 1.资料: 如何用代码设置环境变量?-CSDN论坛.html(ht ...

  4. 浅谈Java简单实现的生产者与消费者问题

    一.面对生产者和消费者的问题,首先我们得明白几点: 生产者:生产数据:消费者:消费数据.消费者在没有数据可供消费的情况下,不能消费:生产者在原数据没有被消费掉的情况下,不能生产新数据.假设,数据空间只 ...

  5. Unity--- 纹理设置属性 alphaIsTransparency

    官方的解释: 意思就是没什么实际效果,只是用做显示用. 参考:https://docs.unity3d.com/ScriptReference/Texture2D-alphaIsTransparenc ...

  6. [Solution] The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path

    HttpServlet需要tomcat等. 右键project点开properties>project facets> 在右侧栏的Runtime tab中勾选tomcat或者新建tomca ...

  7. blast | diamond 输出结果选择和解析 | 比对

    之前的文章:构建NCBI本地BLAST数据库 (NR NT等) | blastx/diamond使用方法 | blast构建索引 | makeblastdb 本地运行blast时,需要指定out fo ...

  8. English Voice of <<See You Again >>

    <See You Again >(<当我们再相见>) 演唱:Wiz Khalifa/Charlie Puth  维兹·卡利法/查理·普斯 It's been a long da ...

  9. English trip V1 - 9.Do you Ever Say Never? 你有没有说永远不会? Teacher:Lamb Key: Adverbs of frequency (频率副词)

    In this lesson you will learn to describe what you do at home. 在本课中,您将学习如何描述您在家中所做的事情. 课上内容(Lesson) ...

  10. 20171104xlVBA制作联合成绩条

    Dim dGoal As Object Dim dCls As Object Sub 制作联合成绩条() Dim sht As Worksheet Dim HeadRng As Range Dim H ...