Cod sursa(job #3160703)
| Utilizator | Data | 24 octombrie 2023 21:38:13 | |
|---|---|---|---|
| Problema | Paduri de multimi disjuncte | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int tata[100001];
int sef(int x)
{
if(tata[x]==x)
return x;
else
return tata[x]=sef(tata[x]);
}
void unire(int x,int y)
{
tata[sef(y)]=sef(x);
}
int main()
{
int n,i,cate,op,x,y;
in>>n>>cate;
for(i=1;i<=n;++i)
tata[i]=i;
for(i=1;i<=cate;++i)
{
in>>op>>x>>y;
if(op==1)
unire(x,y);
else
if(sef(x)==sef(y))
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
}
