Pagini recente » Cod sursa (job #2023038) | Cod sursa (job #2705828) | Cod sursa (job #2333437) | Cod sursa (job #1777490) | Cod sursa (job #1575502)
#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)
{
int rad = x;
while(rad != par[rad])
rad = par[rad];
return rad;
}
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;
}