标签:pac else col getc put algo tchar space cstring
题目:【模板】并查集(洛谷_3367)
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; inline int read(){ int t=1,num=0;char c=getchar(); while(c>‘9‘||c<‘0‘){if(c==‘-‘)t=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){num=num*10+c-‘0‘;c=getchar();} return num*t; } const int N=10010; int fa[N],n,m; void init(){ for(int i=1;i<=n;i++)fa[i]=i; } int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]); } void unite(int x,int y){ x=find(x);y=find(y); if(x==y)return; fa[x]=y; } int main() { n=read();m=read();init(); for(int i=1;i<=m;i++){ int z=read(),x=read(),y=read(); if(z==1)unite(x,y); else{ x=find(x);y=find(y); if(x==y)puts("Y"); else puts("N"); } } return 0; }
本文由Yzyet编写,网址为www.cnblogs.com/Yzyet。非Yzyet同意,禁止转载,侵权者必究。
标签:pac else col getc put algo tchar space cstring
原文地址:http://www.cnblogs.com/Yzyet/p/7623445.html