1346:【例4-7】亲戚(relation)

并查集的模板题:

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 2e4+;
int fa[maxn];
int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void unite(int x, int y)
{
int x1 = find(x);
int y1 = find(y);
if (x1 != y1)fa[x1] = y1;
}
int main()
{
int n, m, x, y;
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i)fa[i] = i;
while (m--)
{
scanf("%d%d", &x, &y);
unite(x, y);
}
scanf("%d", &m);
while (m--)
{
scanf("%d%d", &x, &y);
if (find(x) == find(y))printf("Yes\n");
else printf("No\n");
}
}
上一篇:关于transition和animation


下一篇:Web API系列(二)接口安全和参数校验