【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的标签 官方标签设 ...
随机推荐
- hdu 5724-Chess(状态压缩+sg函数)
hdu 5724-Chess 代码: #include<bits/stdc++.h> using namespace std; ; <<N]; bool S[N]; void ...
- java.lang.NoSuchMethodError问题处理
出现这个问题一般是jar包冲突了,我找了很久没找到是哪个jar冲突了.最后用下面的这段代码,找到是哪个jar冲突了 /** * find jar file */ String LOCATION = & ...
- Python 爬虫-Requests库入门
2017-07-25 10:38:30 response = requests.get(url, params=None, **kwargs) url : 拟获取页面的url链接∙ params : ...
- Python使用base64编码的问题
有的时候,在base64解码的时候,由于字节问题出现解码错误.解决的办法就是不足原base64子串的长度: def decode_base64(data): """ De ...
- Loading Xps from MemoryStream
A common way of loading XpsDocument is to load it from file: XpsDocument document = new XpsDocument( ...
- 3-9《元编程》第3章Tuesday:methods
第3章methods Ruby是动态语言,有静态语言实现不了的技巧.本章讲解代码的重构,把代码变得更简洁. 3.2Dynamic Methods 3.21Calling Methods Dynamic ...
- Confluence 6 使用 LDAP 授权连接到 Confluence 内部目录
希望连接一个内部目录但是使用 LDAP 检查登录授权: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链接. 单击左侧面板上面的 用户目录(User Dire ...
- The Number Games CodeForces - 980E (树, 贪心)
链接 大意: 给定$n$节点树, 求删除$k$个节点, 使得删除后还为树, 且剩余点$\sum{2^i}$尽量大 维护一个集合$S$, 每次尽量添加最大的点即可 这样的话需要支持求点到集合的最短距离, ...
- svn拒绝连接
svn 服务开启: svnserve -d -r /home/svn
- POJ-3009 Curling 2.0 (DFS)
Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But th ...