E. Cyclic Components
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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].

Input

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.

Output

Print one integer — the number of connected components which are also cycles.

Examples
input

Copy
5 4
1 2
3 4
5 4
3 5
output

Copy
1
input

Copy
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
output

Copy
2
Note

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】的更多相关文章

  1. 【Codeforces Round #519 by Botan Investments E】Train Hard, Win Easy

    [链接] 我是链接,点我呀:) [题意] [题解] 设每个人做第一题.第二题的分数分别为x,y 我们先假设没有仇视关系. 即每两个人都能进行一次训练. 那么 对于第i个人. 考虑第j个人对它的贡献 如 ...

  2. 【Codeforces Round #519 by Botan Investments A】 Elections

    [链接] 我是链接,点我呀:) [题意] [题解] 枚举k 那么另外一个人的得票就是nk-sum(ai) 找到最小的满足nk-sum(ai)>sum(ai)的k就ok了 [代码] #includ ...

  3. 【 Codeforces Round #519 by Botan Investments B】Lost Array

    [链接] 我是链接,点我呀:) [题意] [题解] 枚举k 不难根据a得到x[0..k-1] 然后再根据a[k+1..n]来验证一下得到的x是否正确就好. [代码] #include <bits ...

  4. 【Codeforces Round #519 by Botan Investments C】 Smallest Word

    [链接] 我是链接,点我呀:) [题意] [题解] 模拟了一两下.. 然后发现. 对于每一个前缀. 组成的新的最小字典序的字符串 要么是s[i]+reverse(前i-1个字符经过操作形成的最大字典序 ...

  5. 【Codeforces Round #519 by Botan Investments D】Mysterious Crime

    [链接] 我是链接,点我呀:) [题意] 相当于问你这m个数组的任意长度公共子串的个数 [题解] 枚举第1个数组以i为起点的子串. 假设i..j是以i开头的子串能匹配的最长的长度. (这个j可以给2. ...

  6. 【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 ...

  7. 【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 ...

  8. 【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 ...

  9. codeforces标签设置【codeforces内操作, 非浏览器操作】

    直接干货~ 明确需求,关闭标签 步骤: 1.选中上方PROBLEM SET 2.找到Settings  第一个选项是展示未accepted的标签, 第二个选项是隐藏已accepted的标签 官方标签设 ...

随机推荐

  1. Python爬虫Urllib库的基本使用

    Python爬虫Urllib库的基本使用 深入理解urllib.urllib2及requests  请访问: http://www.mamicode.com/info-detail-1224080.h ...

  2. angular5 生命周期钩子函数

    生命周期执行顺序ngOnChanges 在有输入属性的情况下才会调用,该方法接受当前和上一属性值的SimpleChanges对象.如果有输入属性,会在ngOnInit之前调用. ngOnInit 在组 ...

  3. 雷林鹏分享:C# 委托(Delegate)

    C# 委托(Delegate) C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针.委托(Delegate) 是存有对某个方法的引用的一种引用类型变量.引用可在运行时被改变. 委托 ...

  4. English trip -- Review Unit6 Time 时间

    It's at seven o'clock   整点   7点整 It's at half past seven  or  It's seven-thirty7点30 It's at seven fi ...

  5. hdu-1850-nim

    Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32 ...

  6. view_baseInfo

    create view view_baseInfo as select c.spbh,c.tongym, c.spmch,c.shpgg,c.shpchd,a.pihao,a.pici,a.sxrq, ...

  7. 【转】C# 生成二维码并且在中间加Logo(图片合并)

    public class QRCodeHelper { public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidt ...

  8. python之numpy的基本使用

    https://blog.csdn.net/cxmscb/article/details/54583415 一.numpy概述 numpy(Numerical Python)提供了python对多维数 ...

  9. sql存储过程中,如何根据指定日期、月数、天数推算预产日期

    我这边有一个业务,根据某个指定日期,推算某个患者的预产日期 原理:比如孕产的预产日期的算法(预产日期 = 末次月经日期+ 10月+8天) 那么我们怎么通过存储过程来实现呢? 首先分析条件 需要一个指定 ...

  10. Oracle 11g新特性 Interval Partition

    分区(Partition)一直是Oracle数据库引以为傲的一项技术,正是分区的存在让Oracle高效的处理海量数据成为可能,在Oracle 11g中,分区技术在易用性和可扩展性上再次得到了增强.在1 ...