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 ...
随机推荐
- VUE项目实现页面跳转
打开一个VUE项目,目录结构是这样的: 如现在有两个页面aaa和HelloWorld,路由配置在index.js中: import Vue from 'vue' import Router from ...
- MYSQL的基本函数 (加密函数)
AES_ENCRYPT(str,key) 返回用密钥key对字符串str利用高级加密标准算法加密后的结果,调用AES_ENCRYPT的结果是一个二进制字符串,以BLOB类型存储 AES_DECRYP ...
- ionic 搜索双向数据绑定失效
1.用data对象存储变化的数据 js: $scope.data={}; $scope.data.keywords = ""; $scope.search = function() ...
- 虚拟机中不能连接usb设备解决办法
虚拟机中不能连接usb设备解决办法 1.点击开始->运行,在对话框中输入"services.msc",确定,打开windows服务管理器.2.在服务列表中选中"VM ...
- PC端、移动端的页面适配及兼容处理
转自 一.关于移动端兼容性 目前针对跨终端的方案,主要分为两大阵营:一套资源Vs两套资源. 第一种是通过响应式或页面终端判断去实现一套资源适配所有终端: 第二种是通过终端判断分别调取两套资源以适配所有 ...
- LeetCode--342--4的幂
问题描述: 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方. 示例 1: 输入: 16 输出: true 示例 2: 输入: 5 输出: false 进阶:你能不使用 ...
- LeetCode--026--删除排序数组中的重复项
问题描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- CentOS6启动流程
CentOS6启动流程 1.加载BIOS的硬件信息,获取第一个启动设备 在通电之后,CentOS6会进行加电自检(Power On Self Test),这个过程主要是由BIOS完成的.在自检完毕,会 ...
- 远程桌面连接报错:出现身份验证错误,要求函数不受支持,由于CredSSP加密Oracle修正。
远程桌面连接错误: 解决方法: 1.在运行中输入gpedit.msc,启动本地组策略编辑器. 2.定位到计算机—管理模板—系统—凭据分配 3.点凭据分配—加密Oracle修正. 4.加密Oracle修 ...
- spring boot 2.0(二)动态banner的支持
Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜. 配置依赖 使用 Spring Boot 2.0 首先需要将项目依赖包替换为刚刚发 ...