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 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].
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.
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][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的更多相关文章
- Codeforces Round #479 (Div. 3)  E. Cyclic Components  (思维,DFS)
		
题意:给你\(n\)个顶点和\(m\)条边,问它们有多少个单环(无杂环),例如图中第二个就是一个杂环. 题解:不难发现,如果某几个点能够构成单环,那么每个点一定只能连两条边.所以我们先构建邻接表,然后 ...
 - CF 977E Cyclic Components
		
E. Cyclic Components time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
 - Cyclic Components CodeForces - 977E(DFS)
		
Cyclic Components CodeForces - 977E You are given an undirected graph consisting of nn vertices and ...
 - 【codeforces div3】【E. Cyclic Components】
		
E. Cyclic Components time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
 - E. Cyclic Components (DFS)(Codeforces Round #479 (Div. 3))
		
#include <bits/stdc++.h> using namespace std; *1e5+; vector<int>p[maxn]; vector<int&g ...
 - Codeforce 977E Cyclic Components
		
dfs判断图的连通块数量~ #include<cstdio> #include<algorithm> #include<vector> #include<cs ...
 - S - Cyclic Components (并查集的理解)
		
Description You are given an undirected graph consisting of nn vertices and mm edges. Your task is t ...
 - 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 ...
 - codeforce div 377
		
#include <bits/stdc++.h> using namespace std; #define pb push_back #define lb lower_bound #def ...
 
随机推荐
- HTML5-indexedDB使用常见错误总结
			
indexedDB使用过程中常常会出现以下错误: Failed to execute ‘createObjectStore’ on ‘IDBDatabase’: The database is not ...
 - 2016.3.30 OneZero站立会议
			
会议时间:2016年3月30日 13:00~13:20 会议成员:冉华,张敏,王巍,夏一鸣. 会议目的:汇报前一天工作,全体成员评论,确定会议内容或分配下一步任务. 会议内容: 1.汇报头一天工作情 ...
 - oracle数据库添加新用户
			
/*分为四步 */ /*第1步:创建临时表空间 */ create temporary tablespace kmyf_temp tempfile 'E:\app\pangxy\product\11. ...
 - Being a (amateurish) team:团队开发体会
			
0x00 Being a (amateurish) team This is the process of changing hydrogen into breathable oxygen, and ...
 - "留拍"-注册/登录详解
			
1. 注册 打开 “留拍” 软件,进入 主页面 ,然后按 注册 按钮: 在注册页面什么内容 都没有写 上去的情况下,按 完成 按钮: 首先把URL封装起来: public class URL { pu ...
 - 5-Python3从入门到实战—基础之数据类型(列表-List)
			
Python从入门到实战系列--目录 列表定义 list:列表(list)是Python内置的一种数据类型,list是一种有序的集合,索引从0开始,可以进行截取.组合等: //创建列表 list1 = ...
 - Ehcache配置参数示例
			
从Ehcache的jar包里抽取的 <!-- ~ Licensed to the Apache Software Foundation (ASF) under one ~ or more con ...
 - DELPHI XE10,JSON 生成和解析,再利用INDYHTTP控件POST
			
Delphi XE10,Json 生成和解析,再利用indyhttp控件Post 年09月20日 :: 阅读数: --不多说,直接上代码 procedure TFrmMain.Brand; var J ...
 - 用Axios Element 实现全局的请求 loading
			
Kapture 2018-06-07 at 14.57.40.gif demo in github 背景 业务需求是这样子的,每当发请求到后端时就触发一个全屏的 loading,多个请求合并为 ...
 - CentOS7 完整安装后创建私有的yum仓库
			
1. 安装 CentOS7 安装的包比较全,应用可以直接用. 2. 第一步创建 yum 包的存放路径 mkdir -p /var/www/html/ 3. 创建私有仓库 createrepo -v / ...