Graph ' s Cycle ComponentTime
limit:2000/1000 MS (java/others) Memory limit:131072/65536 K (java/others)
Total submission (s): 2112 Accepted Submission (s): 775
Problem DescriptionIn graph theory, a cycle graph is an undirected graph that consists of a single cycle, or in other word s, some number of vertices connected in a closed chain.
Now, you are given a graph where some vertices is connected to is components, can-you figure out how many components is There in the graph and how many of those is cycle graphs.
The vertices belong to a same component if and only if those the vertices connect each other directly or indirectly.
Inputthe input consists of multiply test cases.
The first line of all test case contains, N (0 < n < 100000), m (0 <= M <= 300000), which is the Number of vertices and the number of edges.
The next m lines, each line consists of the integers, U, V, which means there are an edge between U and V.
You can assume this there is no multiply edges and no loops.
The last test case was followed by the zeros, which means the end of input.
Outputfor each test case, output the number of all the components and the number of the which is cycle graphs.
Sample Input
8 90 11 32 30 24 55 76 74 64 72 10 10 0
Sample Output
2 11 0
Author[email protected]
Source2010 acm-icpc multi-university Training Contest (--host by WHU)
Recommendzhouzeyong | We have carefully selected several similar problems for you:3559 3558 3563 3562 3561
AC Code
#include <stdio.h> #include <string.h>int pre[100010],dig[100010];int n,m;/*int Find (int x) {int R=x;while ( R!=PRE[R]) R=pre[r];int j,k;j=x;while (j!=r) {k=pre[j];p re[j]=r;j=k;} return r;} */int Main () {while (scanf ("%d%d", &n,&m)!=eof,n| | m) {//init ();//memset (dig,0,sizeof (Dig)); int I,j;for (i=1;i<=n;i++) {pre[i]=i;dig[i]=0;} for (i=0;i<m;i++) {int u,v;scanf ("%d%d", &u,&v), u++;v++;d ig[u]++;d ig[v]++;//memge (u,v); int Fa=u;while (FA !=PRE[FA]) Fa=pre[fa];int fb=v;while (FB!=PRE[FB]) fb=pre[fb];if (FA!=FB) {if (FA<FB) {PRE[FB]=FA;} ELSEPRE[FA]=FB;}} int Ans1=0,ans2=0;for (i=1;i<=n;i++) {if (pre[i]==i) {ans1++;}} for (i=1;i<=n;i++) {if (dig[i]!=2) {int fi=i;while (Fi!=pre[fi]) {FI=PRE[FI];} pre[fi]=0;//set to 1 will time out,,,,}}for (i=1;i<=n;i++) {if (pre[i]==i) ans2++;} printf ("%d%d\n", ans1,ans2);}}
Hdoj Topic graph ' s Cycle Component (and check set)