Cod sursa(job #2874871)
| Utilizator | Data | 20 martie 2022 13:44:50 | |
|---|---|---|---|
| Problema | Paduri de multimi disjuncte | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.63 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in ("disjoint.in");
ofstream out ("disjoint.out");
int n,m,T[100001],op,x,y;
int Find(int nod)
{
while (T[nod]!=0)
{
nod=T[nod];
}
return nod;
}
void Union(int nod1, int nod2)
{
T[nod2]=nod1;
}
int main()
{
in>>n>>m;
for (int i=1; i<=m; i++)
{
in>>op>>x>>y;
if (op==1)
{
int a=Find(x),b=Find(y);
if (a!=b) Union(a, b);
}
else
{
if (Find(x)==Find(y)) out<<"DA"<<'\n';
else out<<"NU"<<'\n';
}
}
}
