Pagini recente » Cod sursa (job #2296887) | Cod sursa (job #3317560) | Cod sursa (job #3337987) | Cod sursa (job #3310198) | Cod sursa (job #3328813)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX = 100005;
int n, m;
int t[NMAX];
int sz[NMAX];
int root(int nod)
{
/**int y,x;
y = nod;
while(t[nod] != nod)
{
nod = t[nod];
}
while(y!=nod)
{
x=t[y];
t[y]=nod;
y=x;
}
return nod;*/
if(t[nod]==nod) return nod;
t[nod]= root(t[nod]);
return t[nod];
}
void unite(int x, int y)
{
x = root(x);
y = root(y);
if(x!=y)
{
if(sz[x] > sz[y])
swap (x,y);
t[x]=y;
sz[y]+=sz[x];
}
}
int main()
{
int task,x,y;
fin>>n>>m;
for(int i = 1; i <= n; i++)
{
t[i]=i;
sz[i]=1;
}
for(int i = 1; i <= m; i++)
{
fin >> task >> x >> y;
if (task == 1)
{
unite(x,y);
}
else
{
fout << (root(x)==root(y) ? "DA" : "NU") <<"\n";
}
}
return 0;
}