HDU 3926 并查集 图同构简单判断 STL

时间:2021-07-11 04:10:38

给出两个图,问你是不是同构的...

直接通过并查集建图,暴力用SET判断下子节点个数就行了。

/** @Date    : 2017-09-22 16:13:42
* @FileName: HDU 3926 并查集 图同构 连通分量.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e4+20;
const double eps = 1e-8; int fa[N];
int cnt[2][N],clr[2][N]; int find(int x)
{
if(x != fa[x])
fa[x] = find(fa[x]);
return fa[x];
} int join(int a, int b, int f)
{
int x = find(a);
int y = find(b);
if(x != y)
{
fa[y] = x;
cnt[f][x] += cnt[f][y];
cnt[f][y] = 0;
return 1;
}
clr[f][x] = 1;//环标记
return 0;
}
int main()
{
int T;
cin >> T;
int icase = 0;
while(T--)
{
int n, m;
set<PII >s[2];
for(int i = 0; i < 2; i++)
{
cin >> n >> m;
for(int j = 0; j <= n; j++)
fa[j] = j, cnt[i][j] = 1, clr[i][j] = 0;
for(int j = 0; j < m; j++)
{
int x, y;
scanf("%d%d", &x, &y);
join(x, y, i);
}
for(int j = 1; j <= n; j++)//环和非环分组,对照其父亲对于子节点个数
s[i].insert(MP(clr[i][j], cnt[i][j]));
}
/*for(auto i = s[0].begin(), j = s[1].begin(); i != s[0].end(); i++, j++)
cout << i->fi << j->fi << endl;*/ printf("Case #%d: %s\n", ++icase, (s[0]==s[1])?"YES":"NO");
}
return 0;
}//STL大法好!!