#include <iostream>
#include <cstring>
const int N = 200010;
using namespace std;
int n, m, opt, x, y;
int fa[N];
int find(int x)
{
if(x != fa[x]) fa[x] = find(fa[x]);
return fa[x];
}
void Union(int x,int y)
{
int a = find(x);
int b = find(y);
if(a != b) fa[b] = a;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) fa[i] = i;
for (int i = 1; i <= m; i ++ )
{
cin >> opt;
if(opt == 1)
{
cin >> x >> y;
Union(x, y);
}
else
{
cin >> x >> y;
int a = find(x);
int b = find(y);
if(a == b) cout << "Y" << endl;
else cout << "N" << endl;
}
}
}
并查集模板
于 2023-04-07 21:40:12 首次发布