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 ...
随机推荐
- 关于Oracle 12C pdb用户无法登录的问题
新装了oracle12c,对新的CDB和PDB用户如何登录一直一头雾水,经过一晚上的查找,终于解决. sqlplus /nolog -> conn /as sysdba 登录到oracle 将s ...
- 学习笔记44—Linux下安装freesurfer
第一步:安装ubuntu (略过) 第二步:下载freesurfer:从freesurfer的官方网站上下载:http://surfer.nmr.mgh.harvard.edu/fswiki/Down ...
- boostrapt的二级下拉菜单
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <meta conte ...
- 第 2 章 容器架构 - 008 - Docker 组件如何协作?
容器启动过程如下: Docker 客户端执行 docker run 命令. Docker daemon 发现本地没有 httpd 镜像. daemon 从 Docker Hub 下载镜像. 下载完成, ...
- Python学习笔记之参数解析
python提供了两种方法进行命令行的参数解析,分别是getopt和optparse类中的模块OptionParser,下面分别详细了解这两个模块: 1.getopt模块 首先复习C语言的命令行解析: ...
- Enable SMB2 on the Client
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\LanmanWorkstation edit DependOnService and add ...
- GROUP by 方法 C#
1.用两层循环计算,前提条件是数据已经按分组的列排好序的. DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { ...
- 宿主iis部署wcf
WCF学习笔记(4)——宿主iis部署wcf 本文将部署一个wcf+silverlight简单实例,以下是详细步骤: (环境:服务端win2003,iis6.0,asp.net4.0:客户端winXP ...
- ES7: 展开语法spread syntax:
第一次遇到: payload = {...payload, manufacturer: state.manufacturers.filter(x => x._id === payload.man ...
- 「SDOI2008」Sandy 的卡片
用第一个串建立后缀自动机.然后别的串在上面跑.从根节点开始.如果当前不能转移,一直移到slink或者根.如果移到根,能匹配长度变为0,否则变为maxlen[能转移的点]+1,再转移.转移完往slink ...