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. JPA实体关系映射:@ManyToMany多对多关系、@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析

    JPA实体关系映射:@ManyToMany多对多关系.@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析 今天程序中遇到的错误一 org.hibernate.A ...

  2. alias和alias_method的区别:

    1.alias 是 Ruby 的一个关键字,因此使用的时候是 alias :new name :oldname,而 alias_method 是 module 类的一个方法,因此使用的时候是 alia ...

  3. OAuth简介(包含简明使用教程)

    SSO:用户一次登陆后在多个系统免登录. 博客gem 'doorkeeper'  https://i.cnblogs.com/EditPosts.aspx?postid=9255973 OAuth:用 ...

  4. Confluence 6 嵌套用户组的影响

    本部分说明了嵌套用户组对用户登录,权限和查看更新用户组的影响. 登录 如果用户属于一个授权的用户组或者授权用户组中的子用户组,当用户登录后,用户可以访问应用程序. 权限 如果用户属于的用户组或者用户组 ...

  5. Confluence 6 LDAP 连接池配置参数

    初始连接池大小(Initial Pool Size) 当初始化 LDAP 连接池的时候初始化创建的 LDAP 连接数量. 1 期望的连接池大小(Preferred Pool Size) 优化连接池的大 ...

  6. xhost + 的作用

    xhost 是用来控制X server访问权限的. 通常当你从hostA登陆到hostB上运行hostB上的应用程序时, 做为应用程序来说,hostA是client,但是作为图形来说, 是在hostA ...

  7. kaptcha验证码使用

    参数配置: Constant 描述 默认值 kaptcha.border 图片边框,合法值:yes , no yes kaptcha.border.color 边框颜色,合法值: r,g,b (and ...

  8. zabbix自动化监控基础

    zabbix安装配置文档 2 一 zabbix-server 安装配置(基础配置) 2 二 zabbix agent安装配置 5 2.1 主动模式和被动模式 6 2.2 安装配置zabbix_agen ...

  9. jstl <fmt:formatNumber>标签

    标签用于格式化数字,百分比和货币. 如果type属性为百分比或数字,则可以使用多个数字格式属性.maxIntegerDigits和minIntegerDigits属性允许您指定数字的非分数部分的大小. ...

  10. SPOJ UMR 10A 计算几何

    DES:顺时针给出构成凸多边形的点.然后有Q个询问任意给出两个点的编号,询问由这两个点的连线将多边形分成的两部分面积较小的部分面积大小. 比赛时直接每次连线后求多边形求面积超时了.正确解法是求出利用叉 ...