Linus @ 2017-09-26 22:01:10
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
inline int read()
{
char ch = getchar();int f=1,ans=0;
while(ch>'9' || ch<'0'){ if(ch == '-') f = -1,ch = getchar();}
while(ch<='9' && ch>= '0') ans = (ans<<3) + (ans<<1)+ch - '0',ch=getchar();
return f*ans;
}
int f[12345],n,m;
int find(int x)
{
if(f[x] == x)
return x;
else
return f[x] = find(f[x]);
}
int main()
{
freopen("testdata.in","r",stdin);
n=read();m=read();
for(int i=1;i<=n;i++) f[i] = i;
for(int i=1;i<=m;i++)
{
int z=read(),x=read(),y=read();
if(z == 1)
{
int a= find(x),b=find(y);
if(a!=b)
f[a] = b;
}
if(z == 2)
{
if(find(f[x]) == find(f[y]))
printf("Y\n");
else
printf("N\n");
}
}
return 0;
}
by 曾爷爷 @ 2017-10-03 11:18:11
this is mine
:
#include<bits/stdc++.h>
using namespace std;
int n,m,x,y,z,fa[200005];
int getfa(int x)
{
if(x==fa[x]) return fa[x];
return fa[x]=getfa(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++)
{
scanf("%d%d%d",&z,&x,&y);
if(z==1) fa[getfa(x)]=getfa(y);
else
{
if(getfa(x)==getfa(y))puts("Y");
else puts("N");
}
}
}