Pagini recente » Cod sursa (job #1545418) | Cod sursa (job #573906) | Cod sursa (job #1589162) | Cod sursa (job #1312420) | Cod sursa (job #1575499)
#include<fstream>
#include<iostream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
#define MAXN 100005
int n, m, par[MAXN], s[MAXN];
int father(int x) {
if(par[x]==x) return x;
return father(par[x]);
}
void Unite(int x, int y){
if(s[x]==s[y]){
par[x]=y;
s[y]++;
}
if(s[x]>s[y]) par[y]=x;
if(s[y]>s[x]) par[x]=y;
}
int main()
{
fin>>n>>m;
int op, x, y;
for(int i=1; i<=n; ++i) par[i]=i;
while(m--){
fin>>op>>x>>y;
if(op == 1) Unite(father(x), father(y));
else
fout<<(father(x)==father(y)?"DA\n":"NU\n");
}
return 0;
}