码迷,mamicode.com
首页 > 其他好文 > 详细

模板:并查集

时间:2018-07-19 21:27:40      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:int   lse   problem   name   http   bsp   span   targe   color   

 

https://www.luogu.org/problemnew/show/P3367

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n, m, f[10010], z, x, y;
 4 int find(int k){
 5     if(f[k]==k)return k;
 6     return f[k]=find(f[k]);
 7 }
 8 int main(){
 9     cin>>n>>m;
10     for(int i=1; i<=n; i++)
11         f[i]=i;
12     for(int i=1; i<=m; i++){
13         cin>>z>>x>>y;
14         if(z==1)
15             f[find(x)]=find(y);
16         else
17             if(find(x)==find(y))    printf("Y\n");
18             else                    printf("N\n");
19     }
20     return 0;
21 }

 

模板:并查集

标签:int   lse   problem   name   http   bsp   span   targe   color   

原文地址:https://www.cnblogs.com/Aze-qwq/p/9337722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!