Cyclic Components CodeForces - 977E(DFS)
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].
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.
Print one integer — the number of connected components which are also cycles.
5 4
1 2
3 4
5 4
3 5
1
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
2
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)的更多相关文章
- Cyclic Components CodeForces - 977E(找简单环)
题意: 就是找出所有环的个数, 但这个环中的每个点都必须只在一个环中 解析: 在找环的过程中 判断度数是否为2就行...emm... #include <bits/stdc++.h> us ...
- CF 977E Cyclic Components
E. Cyclic Components time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 【codeforces div3】【E. Cyclic Components】
E. Cyclic Components time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- E. Cyclic Components (DFS)(Codeforces Round #479 (Div. 3))
#include <bits/stdc++.h> using namespace std; *1e5+; vector<int>p[maxn]; vector<int&g ...
- Codeforces Round #479 (Div. 3) E. Cyclic Components (思维,DFS)
题意:给你\(n\)个顶点和\(m\)条边,问它们有多少个单环(无杂环),例如图中第二个就是一个杂环. 题解:不难发现,如果某几个点能够构成单环,那么每个点一定只能连两条边.所以我们先构建邻接表,然后 ...
- Codeforces 977E:Cyclic Components(并查集)
题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...
- Codeforce 977E Cyclic Components
dfs判断图的连通块数量~ #include<cstdio> #include<algorithm> #include<vector> #include<cs ...
- 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 ...
随机推荐
- Ubuntu 下 Python自由切换
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo update-alternati ...
- Spring官网下载各版本jar包
1:浏览器输入官网地址:http://spring.io/projects 2:点击如图树叶页面按钮. 3:点击如图小猫图片按钮. 4:查找downloading spring artifacts ...
- 在cmd中登录MySQL数据库
mysql -uroot -p 输入密码,即可
- MySQL Connector/J
5.1 Developer Guide 1. MysQL为由Java语言编程的客户端程序提供连接:MySQL Connector/J,这是一个实现Java Database Connectivity( ...
- JavaScript 第二章总结
Writing real code 设计程序的步骤 First, a high-level design and a flowchart more details Working through th ...
- 雷林鹏分享:C# 基本语法
C# 基本语法 C# 是一种面向对象的编程语言.在面向对象的程序设计方法中,程序由各种相互交互的对象组成.相同种类的对象通常具有相同的类型,或者说,是在相同的 class 中. 例如,以 Rectan ...
- GWAS | 全基因组关联分析 | Linkage disequilibrium (LD)连锁不平衡 | 曼哈顿图 Manhattan_plot | QQ_plot | haplotype phasing
现在GWAS已经属于比较古老的技术了,主要是碰到严重的瓶颈了,单纯的snp与表现的关联已经不够,需要具体的生物学解释,这些snp是如何具体导致疾病的发生的. 而且,大多数病找到的都不是个别显著的snp ...
- Forbidden Subwords
pro: sol: 建出ac自动机. 一个合法的答案对应一条路径满足从一个scc走到另一个scc的路径. 发现这个题的方案数有可能是无限的. 会在以下两种情况无限: 因此,去掉无限情况后,环只有简单环 ...
- 6.4 操作契约 Operation Contracts
4.操作契约 Operation Contracts “用例描述”的补充 强调: 用例中重要的动作,其开始与结束是需要一些约束 4.5 操作契约的后置条件 定义Definition 后 ...
- Tree总结
树结构问题因为容易写出解法,因此经常出现在面试题中 1. 树的种类 1) Tree 2) Binary Trees 3) Binary Search Trees(BST) : used to sort ...