The question is to judge whether two graphs are homogeneous, that is, whether the two graphs are the same. Because the graph of the question is very special and the degree can only be 2, the graph is composed of several links, or several rings, that is, 1 ---> 2 ---> 3 ---> 1, 1 ---, 2 ---> 3; the two graphs are different, special, (1 ---> 1, 2 ---> 2,), 1, 2; the two figures are different; the first one is two self-rings, and the second is two points. This question is about ORZ, ORZ, OTL ,,, weak ....., come on;
[Cpp]
# Include <cstdio>
# Include <algorithm>
# Include <string. h>
# Include <vector>
Using namespace std;
Bool vis [10005];
Vector <int> M [10005];
Constint INF = 9999999;
Struct node
{
Int c_num, r_num;
Int c [10005], r [10005];
} X, y;
Void dfs (int u, int & cnt, int fa, int & flag)
{
Vis [u] = true;
Cnt ++;
Int d = M [u]. size (), v;
For (int I = 0; I <d; I ++)
{
V = M [u] [I];
If (v = fa)
Continue;
If (vis [v])
Flag = 1;
If (! Vis [v])
{
Dfs (v, cnt, u, flag );
}
}
}
Void slove (node & T, int n, int m)
{
Memset (vis, false, sizeof (vis ));
T. r_num = T. c_num = 0;
Memset (T. c, 0x3f, sizeof (T. c ));
Memset (T. r, 0xff, sizeof (T. r ));
For (int I = 0; I <= n; I ++)
M [I]. clear ();
Int a, B;
For (int I = 1; I <= m; I ++)
{
Scanf ("% d", & a, & B );
M [a]. push_back (B );
M [B]. push_back ();
}
For (int I = 1; I <= n; I ++)
{
If (! Vis [I])
{
Int flag = 0;
Int cnt = 1;
Dfs (I, cnt, INF, flag );
If (flag)
{
T. c [T. c_num ++] = cnt;
// Printf ("Case % d shi ring cnt = % d \ n", I, cnt );
} Else {
T. r [T. r_num ++] = cnt;
// Printf ("Case % d shi chain cnt = % d \ n", I, cnt );
}
}
}
}
Bool OK ()
{
// Printf ("1 \ n ");
// Printf ("x. c_num = % d y. c_num = % d \ n", x. c_num, y. c_num );
If (x. c_num! = Y. c_num)
Return false;
Sort (x. c, x. c + x. c_num );
Sort (y. c, y. c + y. c_num );
// Printf ("2 \ n ");
For (int I = 0; I <x. c_num; I ++)
{
If (x. c [I]! = Y. c [I])
Return false;
}
// Printf ("3 \ n ");
If (x. r_num! = Y. r_num)
Return false;
Sort (x. r, x. r + x. r_num );
Sort (y. r, y. r + y. r_num );
// Printf ("4 \ n ");
For (int I = 0; I <x. r_num; I ++)
If (x. r [I]! = Y. r [I])
Return false;
// Printf ("5 \ n ");
Return true;
}
Int main ()
{
Int t;
Scanf ("% d", & t );
For (int I = 1; I <= t; I ++)
{
Int n, m;
Scanf ("% d", & n, & m );
Slove (x, n, m );
Scanf ("% d", & n, & m );
Slove (y, n, m );
If (OK ())
Printf ("Case # % d: YES \ n", I );
Else
Printf ("Case # % d: NO \ n", I );
}
Return 0;
}
Author: Java_beginer1