yi_shang @ 2019-09-01 22:19:14
#include <iostream>
#include <cstdio>
using namespace std;
int n, m;
int x, y, z;
int father[10001];
int find_father(int y)
{
if (father[y] == y)
return y;
return find_father(father[y]);
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= 10001; i++)
father[i] = i;
for (int i = 1; i <= m; i++)
{
cin >> z >> x >> y;
if (z == 1)
father[x] = find_father(y);
else
{
if (find_father(x) == find_father(y))
cout << "Y" << endl;
else
cout << "N" << endl;
}
}
cout << endl;
return 0;
}
并查集应该没问题吧
by VenusM1nT @ 2019-09-01 22:21:50
int Find(reg int x)
{
return x==f[x]?x:f[x]=Find(f[x]);
}
您似乎少了这个 ↑
by HoshinoTented @ 2019-09-01 22:22:22
应该是 father[find_father(x)] = find_father(y)
吧
顺便说一句没有路径压缩
by first_fan @ 2019-09-01 22:25:15
(和Venus的一行并查集一模一样可还行
by dbxxx @ 2019-10-09 22:55:56
father[find_father(x)] = find_father(y)