Graph’s Cycle Component(并查集优化)
来源:互联网 发布:ubuntu 16.04 jenkins 编辑:程序博客网 时间:2024/05/22 07:55
Now, you are given a graph where some vertices are connected to be components, can you figure out how many components are there in the graph and how many of those components are cycle graphs.
Two vertices belong to a same component if and only if those two vertices connect each other directly or indirectly.
input
The input consists of multiply test cases.
The first line of each test case contains two integer, n (0 < n < 100000), m (0 <= m <= 300000), which are the number of vertices and the number of edges.
The next m lines, each line consists of two integers, u, v, which means there is an edge between u and v.
You can assume that there is no multiply edges and no loops.
The last test case is followed by two zeros, which means the end of input.
output
For each test case, output the number of all the components and the number of components which are cycle graphs
sample input
8 9
0 1
1 3
2 3
0 2
4 5
5 7
6 7
4 6
4 7
2 1
0 1
0 0
sample output
2 1
1 0
题意就是找出无向图中有几个连通块,有几个环
解法还是并查集,但是环需要加一个标记数组来确定,具体的直接看代码吧。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int map[110000],in[110000];int x,y,n,m,a,b;int find(int x){ return map[x]==x?x:find(map[x]);}void merge(int a,int b){ int p=find(a); int q=find(b); if(p<q) map[q]=p; else map[p]=q;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { if(m==0&&n==0) break;for(int i=1;i<=n;i++) map[i]=i; memset(in,0,sizeof(in)); for(int i=1;i<=m;i++) { scanf("%d%d",&a,&b); a++;b++; in[a]++; in[b]++; merge(a,b); } x=0; y=0; for(int i=1;i<=n;i++) if(map[i]==i) x++; for(int i=1;i<=n;i++)if(in[i]!=2) map[find(i)]=0; for(int i=1;i<=n;i++) if(map[i]==i) y++; printf("%d %d\n",x,y); }}
- Graph’s Cycle Component(并查集优化)
- HDOJ 题目Graph’s Cycle Component(并查集)
- Graph’s Cycle Component (并查集)
- HDU 3560 Graph’s Cycle Component(并查集)
- HDU 3560 Graph’s Cycle Component 【并查集】
- HDU 3560 并查集 Graph’s Cycle Component
- hdu 3560 Graph’s Cycle Component 并查集
- HDU 3560--Graph’s Cycle Component【并查集(判环)】
- hdu3560Graph’s Cycle Component 并查集
- Graph’s Cycle Component
- HDU3560 Graph’s Cycle Component
- B. Mr. Kitayuta's Colorful Graph(并查集)
- Mr. Kitayuta's Colorful Graph(巧用并查集)
- 505B. Mr. Kitayuta's Colorful Graph(拆分图+并查集)
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph (并查集 STL)
- Rikka with Graph(并查集)
- hihocoder1629:Graph (分块+并查集)
- Connected Component 并查集
- Python笔记(二)--变量类型
- 在没有用户ID或者任何唯一性标识的情况下做个唯一标识
- springboot无法完成变量从pom到.properties文件自动替换的问题
- 可并堆(左偏树)简单学习
- QT 5.6 自带的数据库QSqlite 使用实例
- Graph’s Cycle Component(并查集优化)
- 方差计算过程中分母为什么不是n?
- gitlab备份、恢复、迁移、升级
- 1005.Number Sequence
- H5随笔第一课
- 关于ArcGlobe动画的一些心得
- laravel 服务容器实例——深入理解IoC模式
- ##ECMAScript5补充
- android margin