Pagini recente » Cod sursa (job #2690119) | Cod sursa (job #1675196) | Cod sursa (job #136680) | Cod sursa (job #1583541) | Cod sursa (job #2693339)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int lim = 100005;
int n,m,op,t[lim];
int Root(int x)
{
if(x == t[x]) return x;
else return t[x] = Root(t[x]); //leaga fiecare nod de pe drumul format de radacina si nodul x, de radacina
}
void Unite(int a ,int b)
{
t[a] = b;
}
int main()
{
fin >> n >> m;
for(int i=1 ; i<=n ; i++)
t[i]=i ;
for(int i=1 ; i<=m ; i++)
{
int op,x,y;
fin >> op >> x >> y;
if(op == 1)
{
int a = Root(x);
int b = Root(y);
if(a != b)
Unite(a , b);
}
else
if(Root(x) == Root(y)) fout<<"DA"<<'\n';
else fout<<"NU"<<'\n';
}
return 0;
}