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

P3367 【模板】并查集

时间:2020-06-28 20:32:04      阅读:33      评论:0      收藏:0      [点我收藏+]

标签:ble   ace   ons   div   amp   ==   using   tps   const   

P3367 【模板】并查集

#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int a[N];
int n,m;
int fa[N];

int find(int x){
	if(fa[x]==x){
		return x;
	}
	fa[x]=find(fa[x]);
	return fa[x];
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		fa[i]=i;
	}
	for(int i=1;i<=m;i++){
		int z,x,y;
		scanf("%d%d%d",&z,&x,&y);
		if(z==1){
			fa[find(x)]=find(y);
		} else {
			if(find(x)==find(y)){
				printf("Y\n");
			} else {
				printf("N\n");
			}
		}
	}
	return 0;
}

 

P3367 【模板】并查集

标签:ble   ace   ons   div   amp   ==   using   tps   const   

原文地址:https://www.cnblogs.com/LightyaChoo/p/13204731.html

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