Relisc @ 2024-07-05 17:34:28
#include <bits/stdc++.h>
using namespace std;
const int N=1e6;
int fa[N];
int n,m;
//查找父节点
int find(int x)
{
if(fa[x] != x) fa[x]=find(fa[x]);
return fa[x];
}
//路径压缩
int main(){
cin>>n>>m;
//并查集初始化
for(int i=1;i<=n;i++) fa[i]=i;
while(m--)
{
int w,u,v;
cin>>w>>u>>v;
//路径压缩,为什么这里不加大括号会wa?
if(w == 1)
{
if(find(u) != find(v)) fa[find(u)]=find(v);
}
else if(w == 2)
{
if( find(u) == find(v) ) cout<<"Y"<<endl;
else cout<<"N"<<endl;
}
}
return 0;
}
by ZMQ_Ink6556 @ 2024-07-05 17:36:52
不加会把
if(find(u) != find(v)) fa[find(u)]=find(v);
和
else if(w == 2)
识别成一组 if-else
语句。@Relisc
by Relisc @ 2024-07-05 19:29:57
@zhangmoqing 看明白了!谢谢!