Wen_SuHuai @ 2022-05-14 13:40:55
#include<bits/stdc++.h>
using namespace std;
int par[10010],rank[10010];
void init(int n){
for(int i = 0 ; i < n ; i++){
par[i] = i;
rank[i] = 0;
}
}
int find(int x){
if(par[x] == x) return x;
else return par[x] = find(par[x]);
}
void unite(int x,int y){
x = find(x);
y = find(y);
if(x == y) return;
if(rank[x] == rank[y]) rank[x]++;
if(rank[x] < rank[y]) par[x] = y;
else par[y] = x;
}
int main(){
int N,M,Z,X,Y;
cin>>N>>M;
init(1001);
for(int i = 0 ; i < M ; i++){
cin>>Z>>X>>Y;
if(Z == 1) unite(X,Y);
if(Z == 2){
if(find(X) == find(Y)) cout<<'Y'<<"\n";
else cout<<'N'<<"\n";
}
}
}
by Wen_SuHuai @ 2022-05-14 13:42:03
奇奇怪怪的WA 本蒟蒻等待各位dalao帮助
by zsc2003 @ 2022-05-14 13:45:14
你应该
init(10001);
或者直接
init(N+1);
by Wen_SuHuai @ 2022-05-14 13:51:26
蟹蟹了 问题已解决