Dog_Two @ 2017-11-27 20:42:08
#include<bits/stdc++.h>
using namespace std;
int n,m;
int num1,num2;
int flag;
map<int,int>fa;
int getfa(int a){return !fa.count(a)||fa[a]==a?fa[a]=a:fa[a]=getfa(fa[a]);}
bool equal(int a,int b){return getfa(a)==getfa(b);}
void uni(int a,int b){fa[getfa(a)]=getfa(b);}
int main()
{
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d%d",&flag,&num1,&num2);
if(flag==1)
uni(num1,num2);
else
printf(equal(num1,num2)?"Y\n":"N\n");
}
return 0;
}
by D10s @ 2017-11-29 19:59:06
为什么要用map呢……数组不是蛮好的
by Dog_Two @ 2017-12-01 22:05:45
@D10s 我在想能不能用一种骚操作避免掉最开始的 fa[i]=i;······
by 密期望 @ 2018-02-11 21:30:06
class node{
public:
node*father;
node()
{
father=this;
}
node*find();
node*link(node*);
}
by 密期望 @ 2018-02-11 21:30:36
完了,差了个分号