Graph’s Cycle Component
来源:互联网 发布:什么叫知世故而不世故 编辑:程序博客网 时间:2024/06/05 11:20
描述:
In graph theory, a cycle graph is an undirected graph that consists of a single cycle, or in other words, some number of vertices connected in a closed chain.
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.
输出:
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.
输出:
For each test case, output the number of all the components and the number of components which are cycle graphs.
样例输出:
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
样例输出:
2 1
1 0
题目大意:
您将获得一些图形,其中某些顶点连接到组件,您可以确定图中有多少组件,以及这些组件中有多少是循环图。当且仅当这两个顶点直接或间接连接时,两个顶点属于相同的组件。
#include<stdio.h>#include<string.h>int map[110000],asd[110000];int n,m;int find(int x)//查找祖先结点 { return x==map[x]?x:find(map[x]);}void combil(int a,int b)//建立连接使得有共同祖先 { int p=find(a); int q=find(b);if(p!=q){if(p<q) map[q]=p; else map[p]=q;}}int main(){int a,b,cycle,conponents; while(scanf("%d %d",&n,&m),n) { for(int i=1;i<=n;i++) map[i]=i; memset(asd,0,sizeof(asd)); for(int i=1;i<=m;i++) { scanf("%d %d",&a,&b); asd[++a]++;//注意要先加加 asd[++b]++; combil(a,b); } conponents=0; cycle=0; for(int i=1;i<=n;i++) { if(map[i]==i) conponents++;} for(int i=1;i<=n;i++) { if(asd[i]!=2)//如果个数不是2就一定不是组件 map[find(i)]=0; } for(int i=1;i<=n;i++){ if(map[i]==i) cycle++;} printf("%d %d\n",conponents,cycle); } return 0;}
- Graph’s Cycle Component
- HDU3560 Graph’s Cycle Component
- HDU 3560 Graph’s Cycle Component 【并查集】
- HDU 3560 并查集 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 并查集
- Graph’s Cycle Component(并查集优化)
- HDU 3560--Graph’s Cycle Component【并查集(判环)】
- 多校_WHU_Graph’s Cycle Component hdoj3560
- hdu3560Graph’s Cycle Component 并查集
- Flex Component Life Cycle
- Cycle in Graph (coder)
- Connected Component in Undirected Graph
- Unity's Life cycle
- 组件的生存周期Component Life Cycle
- 263D - Cycle in Graph 图论 YY
- Android SystemServer启动流程源码解析
- Android--签名制作
- Java对象序列化和反序列化
- 数据库为什么要分库分表
- js file上传图片并显示出来
- Graph’s Cycle Component
- Python安装nltk以及里面一些包的使用
- NYOJ 214 单调递增子序列(二)(二分加dp)
- 实现一个函数把每个空格替换成20%
- 【mysql 卡死处理】MYSQL--慢查询卡死等处理
- Flask——itsdangerous验证邮箱
- 【技术资讯】黑户该怎么申请个人贷款?
- Linux 防火墙开放特定端口 (iptables)
- Java静态代理和动态代理