• 洛谷P3367并查集


    传送门

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <map>
    #define re register
    using namespace std;
    const int maxn = 10005;
    const int maxm = 200005;
    
    inline int read(){
    	char ch = getchar();
    	int f = 1 , x = 0 ;
    	while(ch > '9' || ch < '0'){if(ch == '-') f = -1;ch = getchar();}
    	while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0'; ch = getchar();}
    	return x * f;
    }
    
    int n,m,z,x,y;
    int fa[maxn];
    
    inline int find(int x){
    	if(x != fa[x])  fa[x] = find(fa[x]);
    	return fa[x];
    }
    
    inline void init(){
    	for(re int i = 1 ; i <= n ; ++i)
    		fa[i] = i;
    }
    
    int main(){
    	n = read(); m = read();
    	init();
    	for(re int i = 1 ; i <= m ; ++i) {
    		z = read(); x = read(); y = read();
    		if(z == 1) {
    			int f1 = find(x) , f2 = find(y);
    			if(f1 != f2)
    				fa[f1] = f2;
    		}
    		else {
    			int f1 = find(x) , f2 = find(y);
    			if(f1 == f2) printf("Y
    ");
    			else printf("N
    ");
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    高精度乘法
    阶乘
    高精度减法
    高精度加法
    曹冲养猪
    采药2
    nginx.conf详解
    系统盘脚本扩容
    IDEA中编写脚本并运行shell脚本
    常用的pdf工具
  • 原文地址:https://www.cnblogs.com/Stephen-F/p/9931872.html
Copyright © 2020-2023  润新知