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 ...
随机推荐
- zabbix问题记录
zabbix部署好,在使用一段时间后,出现了不少报错,在此简单做一记录.1)Zabbix监控界面报错Lack of free swap space on Zabbix server”解决公司线上部署的 ...
- individual project1 12061183
1.项目预计用时 之前大二下学期的时候学过面向对象,当时老师叫我们写过一个统计目录下单词的程序,大致的思路是一样的.于是觉得这个程序并不难写.于是就在周末还很轻松地休息着不看程序,知道别的同 ...
- SCRUM 12.22
周一,大家现在课程也比较少,今天都在非常努力地写代码. 任务分配如往常一样,我们现在基本将工作的重心放在完善已有的组件上. 成员 任务 彭林江 落实API 牛强 落实意见反馈功能测试 高雅智 测试已完 ...
- 个人博客作业Week7(阅读文章,心得体会)
Alpha阶段结束了,内心可以说是五味杂陈.不是说我们的产品拿不上台面那般差劲,复杂的心绪主要来源于和别的队的比较,别的队才刚刚发布没多久访问量和注册量就破百了,并且还发起了找bug送红包的活动.可能 ...
- 单工程搭建springmvc+spring+mybatis(maven,idea)
单工程搭建springmvc+spring+mybatis(maven,idea) 1.pom.xml <properties> <project.build.sourceEncod ...
- 第三个spring冲刺第5、6、7天(三天汇总)
这三天我们一直在研究选择题的选项插入与切换,现在已经大致完善了,达到了预想的效果,晚点会补上截图.
- maven配置私服
1先配置maven的配置文件 2在项目的pom.xml文件增加 <distributionManagement> <repository> <id>nexus-re ...
- 【Alpha发布】网站已经正式发布!
Alpha版本发布说明 一.功能介绍 本团队所做的物理实验网站是以生成物理实验报告为基础功能的网站.Alpha版本具有的功能大体如下: Figure 1首页 1. 注册登录功能 用户可以通过在注册页通 ...
- Running kubernetes on windows
docker-for-desktop minikube GKE cluster(?) docker-for-desktop https://docs.docker.com/docker-for-win ...
- 批处理-For详解
大纲 一 前言 二 for语句的基本用法 三 for /f (delims.tokens.skip.eol.userbackq.变量延迟) 四 for /r (递归遍历) 五 for /d (遍历目录 ...