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;
}