HDU 3560 and query the Graph's Cycle Component

Source: Internet
Author: User

Analysis: this also requires the number of simple loops, as long as a tag array is added, it will not be a set tag of simple loops.


[Cpp]
# Include <iostream>
# Include <string>
# Include <cstring>
# Include <algorithm>
# Include <cstdio>
# Include <cmath>
# Include <iomanip>
 
Using namespace std;
Const int maxn = 100000;
Int f [maxn];
Int ds [maxn];
Bool vis [maxn];
 
Int find_set (int x ){
If (f [x]! = X) f [x] = find_set (f [x]);
Return f [x];
}
Void union_set (int x, int y ){
X = find_set (x );
Y = find_set (y );
If (x! = Y) f [y] = x;
}
Int main (){
Int n, m;
While (~ Scanf ("% d", & n, & m), n | m ){
Memset (ds, 0, sizeof (ds ));
Memset (vis, true, sizeof (vis ));
For (int I = 0; I <n; ++ I) f [I] = I;
While (m --){
Int a, B; scanf ("% d", & a, & B );
Ds [a] ++; ds [B] ++;
Union_set (a, B );
}
For (int I = 0; I <n; ++ I)
If (ds [I]! = 2) vis [find_set (I)] = false;
Int s = 0, h = 0;
For (int I = 0; I <n; ++ I)
If (find_set (I) = I ){
S ++;
If (vis [I]) h ++;
}
Printf ("% d \ n", s, h );
}
Return 0;
}

# Include <iostream>
# Include <string>
# Include <cstring>
# Include <algorithm>
# Include <cstdio>
# Include <cmath>
# Include <iomanip>

Using namespace std;
Const int maxn = 100000;
Int f [maxn];
Int ds [maxn];
Bool vis [maxn];

Int find_set (int x ){
If (f [x]! = X) f [x] = find_set (f [x]);
Return f [x];
}
Void union_set (int x, int y ){
X = find_set (x );
Y = find_set (y );
If (x! = Y) f [y] = x;
}
Int main (){
Int n, m;
While (~ Scanf ("% d", & n, & m), n | m ){
Memset (ds, 0, sizeof (ds ));
Memset (vis, true, sizeof (vis ));
For (int I = 0; I <n; ++ I) f [I] = I;
While (m --){
Int a, B; scanf ("% d", & a, & B );
Ds [a] ++; ds [B] ++;
Union_set (a, B );
}
For (int I = 0; I <n; ++ I)
If (ds [I]! = 2) vis [find_set (I)] = false;
Int s = 0, h = 0;
For (int I = 0; I <n; ++ I)
If (find_set (I) = I ){
S ++;
If (vis [I]) h ++;
}
Printf ("% d \ n", s, h );
}
Return 0;
}

 

Related Article

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.