/*
* @Description: To iterate is human, to recurse divine.
* @Autor: Recursion
* @Date: 2022-03-18 19:30:16
* @LastEditTime: 2022-03-18 19:41:28
*/
#include<bits/stdc++.h>
using namespace std;
int N,M;
int f[(int)1e6+10];
int find(int x)
{
if(f[x] == x)
return x;
else
return f[x] = find(f[x]);
}
int main()
{
int x,y,z;
while(cin >> N >> M){
for(int i = 1;i <= N;i ++)
f[i] = i;
for(int i = 1;i <= M;i ++){
cin >> z >> x >> y;
if(z == 1){
f[find(x)] = find(y);
}
else{
if(find(x) == find (y))
cout << "Y" << endl;
else
cout<< "N" << endl;
}
}
}
}
P3367 【模板】并查集
最新推荐文章于 2025-12-19 15:31:04 发布
845

被折叠的 条评论
为什么被折叠?



