Mai intai trebuie sa te autentifici.
Cod sursa(job #2805701)
| Utilizator | Data | 21 noiembrie 2021 19:12:56 | |
|---|---|---|---|
| Problema | Paduri de multimi disjuncte | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.62 kb |
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n, m, t[100001], r[100001];
void read()
{
in >> n >> m;
for(int i = 1; i <= n; ++i)
t[i] = i;
}
int root(int x)
{
if(t[x] == x)
return x;
return root(t[x]);
}
void U(int x, int y)
{
if(r[x] < r[y]) t[x] = y;
else if(r[x] > r[y]) t[y] = x;
else t[y] = x, r[x]++;
}
void afis()
{
int cod, x, y;
while(m--)
{
in >> cod >> x >> y;
if(cod == 1) U(root(x), root(y));
else out << (root(x) == root(y) ? "DA\n" : "NU\n");
}
}
int main()
{
read();
afis();
return 0;
}