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

洛谷 P3367 并查集模板

时间:2017-04-12 23:31:13      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:cst   names   http   https   void   code   clu   www   span   


#include<cstdio> using namespace std; int n,m,p; int father[2000001]; int find(int x) { if(father[x]!=x) father[x]=find(father[x]); return father[x]; } void unionn(int i,int j) { father[j]=i; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) father[i]=i; for(int i=1;i<=m;i++) { int x,y,z,r1,r2; scanf("%d%d%d",&z,&x,&y); if(z==1) { r1=find(x); r2=find(y); if(r1!=r2) unionn(r1,r2); } if(z==2) { if(find(x)==find(y)) printf("Y\n"); else printf("N\n"); } } return 0; }

洛谷的网站编排对题目粘贴不友好,所以没题目

链接在这:https://www.luogu.org/problem/show?pid=3367

洛谷 P3367 并查集模板

标签:cst   names   http   https   void   code   clu   www   span   

原文地址:http://www.cnblogs.com/kuaileyongheng/p/6701605.html

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