#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std ;
const int N = 8e5 +10 ;
int s[N] ;
int n , m;
int find(int x){//压缩路径,找祖宗结点
if(x!=s[x]) s[x] = find(s[x]) ;
return s[x] ;
}
int main(){
cin >> n >> m ;
for(int i = 1 ; i <= n ;i ++) s[i] = i ;
for(int i = 1 ; i <= m ; i ++){
int o , a , b ;
cin >> o >> a >> b ;
if(o == 1){//并查集的合并,找a 和 b 的祖宗节点,如果两个祖宗不一样,那就让其中一个的祖宗变成了另一个的祖宗的祖宗
int x = find(a) , y = find(b) ;
if(x!=y) s[x] = y ;
}
if(o == 2){
if(find(a) == find(b)) cout << "YES" <<endl ;
else cout << "NO" << endl ;
}
}
}
lanqiao oj 1135 蓝桥幼儿园(并查集)
最新推荐文章于 2024-07-23 17:36:37 发布