[并查集]B. 2.约束条件
解析
由于数据太大了, \(10^9\) 的内存绝对会炸, 所以要用离散化
Code
#include <bits/stdc++.h>
using namespace std;
int n, m, z, x, y;
int f[10005];
int find (int x)
{
while (x != f[x]) x = f[x] = f[f[x]];
return x;
}
int main ()
{
scanf ("%d%d", &n, &m);
for (int i = 1; i <= n; ++ i) f[i] = i;
for (int i = 1; i <= m; ++ i)
{
scanf ("%d%d%d", &z, &x, &y);
int a = find (x), b = find (y);
if (z == 1) f[a] = f[b];
else
{
if (a == b) printf ("Y\n");
else printf ("N\n");
}
}
return 0;
}