Cod sursa(job #1846895)
Utilizator | Ungureanu Mihnea UMihnea | Data | 14 ianuarie 2017 09:23:58 |
---|---|---|---|
Problema | Paduri de multimi disjuncte | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.54 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int T[100010], n, m, cod, i, x, y;
int compresie(int x)
{
if(T[x] == x) return x;
T[x] = compresie(T[x]);
return T[x];
}
int main()
{
f >> n >> m;
for(i = 1; i <= n; i++)
T[i] = i;
for(; m; m--)
{
f >> cod >> x >> y;
x = compresie(x);
y = compresie(y);
if(cod == 1) T[x] = y;
else x == y ? g << "DA\n" : g << "NU\n";
}
return 0;
}