【codeforces div3】【E. Cyclic Components】
2 seconds
256 megabytes
standard input
standard output
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
#include <bits/stdc++.h>
using namespace std; #define f first
#define s second
#define ll long long
const int maxn=3e5;
vector<int>v[maxn];
int vis[maxn];
int ans,flag;
void dfs(int now,int fa)
{
vis[now]=;
if(v[now].size()!=)flag=;
for(auto i:v[now])
{
if(i==fa||vis[i])continue;
dfs(i,now);
}
} int main()
{
int n,m;
cin>>n>>m;
for(int i=;i<m;i++)
{
int x,y;
cin>>x>>y; v[x].push_back(y);
v[y].push_back(x);
}
for(int i=;i<=n;i++)
{
flag=;
int ok=;
if(!vis[i])dfs(i,-),ok=;
if(flag==&&ok==)ans++;
}
cout<<ans;
return ;
}
【方法二】
方法一中的dfs仅仅是寻找连在一起的点,其实寻找一个连通块连在一起的点只需要使用并查集就能解决,以下是并查集+判断度是不是为2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=;
int fa[maxn];
vector<int>qwq[maxn];
vector<int>orz[maxn];
int find(int x)
{
int xx=x;
while(x!=fa[x])
{
x=fa[x];
}
while(fa[xx]!=x)
{
int t=fa[xx];
fa[xx]=x;
xx=t;
}
return x;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i = ; i <= n ;i++)
fa[i]=i;
while(k--)
{
int a,b;
scanf("%d%d",&a,&b);
qwq[a].push_back(b);
qwq[b].push_back(a);
int qaq1=find(a);
int qaq2=find(b);
if(qaq1!=qaq2);
fa[qaq1]=qaq2;
}
for(int i = ; i <= n ; i++)
{
orz[find(i)].push_back(i);//利用连通块所有点的祖先来将联通块内部的点存在一起
}
int cnt=;
for(int i = ; i <= n ; i++)
{
if(orz[i].size()>)
{
bool or2=;
for(int j = ; j < orz[i].size()&&or2;j++)
{
if(qwq[orz[i][j]].size()!=)or2=;
}
if(or2)cnt++;
}
}
printf("%d\n",cnt);
return ;
}
【codeforces div3】【E. Cyclic Components】的更多相关文章
- 【Codeforces Round #519 by Botan Investments E】Train Hard, Win Easy
[链接] 我是链接,点我呀:) [题意] [题解] 设每个人做第一题.第二题的分数分别为x,y 我们先假设没有仇视关系. 即每两个人都能进行一次训练. 那么 对于第i个人. 考虑第j个人对它的贡献 如 ...
- 【Codeforces Round #519 by Botan Investments A】 Elections
[链接] 我是链接,点我呀:) [题意] [题解] 枚举k 那么另外一个人的得票就是nk-sum(ai) 找到最小的满足nk-sum(ai)>sum(ai)的k就ok了 [代码] #includ ...
- 【 Codeforces Round #519 by Botan Investments B】Lost Array
[链接] 我是链接,点我呀:) [题意] [题解] 枚举k 不难根据a得到x[0..k-1] 然后再根据a[k+1..n]来验证一下得到的x是否正确就好. [代码] #include <bits ...
- 【Codeforces Round #519 by Botan Investments C】 Smallest Word
[链接] 我是链接,点我呀:) [题意] [题解] 模拟了一两下.. 然后发现. 对于每一个前缀. 组成的新的最小字典序的字符串 要么是s[i]+reverse(前i-1个字符经过操作形成的最大字典序 ...
- 【Codeforces Round #519 by Botan Investments D】Mysterious Crime
[链接] 我是链接,点我呀:) [题意] 相当于问你这m个数组的任意长度公共子串的个数 [题解] 枚举第1个数组以i为起点的子串. 假设i..j是以i开头的子串能匹配的最长的长度. (这个j可以给2. ...
- 【Codeforces Round #505 (Div. 1 + Div. 2) 】
A:https://www.cnblogs.com/myx12345/p/9843966.html B: C:https://www.cnblogs.com/myx12345/p/9844084.ht ...
- 【Codeforces Round #504 (Div. 1 + Div. 2) 】
A:https://www.cnblogs.com/myx12345/p/9843678.html B:https://www.cnblogs.com/myx12345/p/9843709.html ...
- 【Codeforces Round #502 (Div. 1 + Div. 2) 】
A:https://www.cnblogs.com/myx12345/p/9843032.html B:https://www.cnblogs.com/myx12345/p/9843050.html ...
- codeforces标签设置【codeforces内操作, 非浏览器操作】
直接干货~ 明确需求,关闭标签 步骤: 1.选中上方PROBLEM SET 2.找到Settings 第一个选项是展示未accepted的标签, 第二个选项是隐藏已accepted的标签 官方标签设 ...
随机推荐
- Codeforces 913C - Party Lemonade
913C - Party Lemonade 思路:对于第i个话费cost[i],取min(cost[i],2*cost[i-1]),从前往后更新,这样就可以保证第n个的话费的性价比最高,那么从最高位开 ...
- MongoDB分片集群环境搭建记录
--创建配置服务器mongod.exe --logpath "G:\USERDATA\MONGODB\Test2\Log\mongodb.log" --logappend --db ...
- Greengenes Database(16S)
The Greengenes Database Release 13_5 这是16S的一个非常重要的数据库 The Greengenes Database, a public resource sin ...
- 测序中Q20 Q30 Q40
你能给别人讲清楚这个概念吗? 二代测序中,每测一个碱基会给出一个相应的质量值,这个质量值是衡量测序准确度的.碱基的质量值13,错误率为5%,20的错误率为1%,30的错误率为0.1%.行业中Q20与Q ...
- axios构建请求池处理全局loading状态&&axios避免重复请求
很多时候我们能够看到类似进度条一样的东西在页面顶部进行加载,代表页面是否加载完成,或者其他的loading效果,我们当然不可能通过promise.all来讲所有的请求合并到一起然后进行处理,这个时候我 ...
- Python在七牛云平台的应用(三)简单的人脸识别
前言 这是最后一篇介绍python在七牛云平台的应用了,因为-前两篇文章第一篇分享了怎么安装七牛的官方库以及怎么对自己的空间进行下载上传,删除等行动.而第二篇则分享了怎么利用七牛的API接口,由于七牛 ...
- 『OpenCV3』简单图片处理
cv2和numpy深度契合,其图片读入后就是numpy.array,只不过dtype比较不常用而已,支持全部数组方法 数组既图片 import numpy as np import cv2 img = ...
- 秒杀多线程第三篇 原子操作 Interlocked系列函数
上一篇<多线程第一次亲密接触 CreateThread与_beginthreadex本质区别>中讲到一个多线程报数功能.为了描述方便和代码简洁起见,我们可以只输出最后的报数结果来观察程序是 ...
- WebSocket教程(一)
一.websocket与http WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化,或者说没关系,但HTTP是不支持持久连接的(长连接,循环连接的不算) 首先HTTP有 1 ...
- silent install oracle 11.2.0.1 x86_64 for linux
su - root#groupadd oinstall#useradd -g oinstall oracle#passwd oracle#mkdir -p /u01/app/oracle#chown ...