wenhanzheng @ 2024-08-07 10:46:40
#include "bits/stdc++.h"
using namespace std;
int n, m;
int arr[10005];
int find(int x);
int main(){
cin >> n >> m;
for (int i = 1; i <= n; i++) arr[i] = i;
for (int i = 1; i <= m; i++){
int a, b, c;
cin >> a >> b >> c;
if(a == 1){
arr[find(c)] = find(b);
}
else{
if(find(b) != find(c)) cout << "N\n";
else cout << "Y\n";
}
}
}
int find(int x){
while(x != arr[x]){
x = arr[x];
}
return x;
}
TLE了3个点 求调
by Dangerise @ 2024-08-07 10:50:27
@wenhanzheng
回去学一下路径压缩优化
int find(int x){
if(arr[x]==x){
return x;
}else{
return arr[x]=find(arr[x]);
}
}
by wenhanzheng @ 2024-08-07 10:55:23
@Dangerise 谢谢dalao