Hdoj Topic graph ' s Cycle Component (and check set)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.