Cod sursa(job #2062578)
| Utilizator | Data | 10 noiembrie 2017 16:46:45 | |
|---|---|---|---|
| Problema | Paduri de multimi disjuncte | Scor | 70 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.66 kb |
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int NMax = 100000;
int TT[NMax + 5];
int N,M;
int Root(int x)
{
while(TT[x] != x)
x = TT[x];
return x;
}
void Unite(int x, int y)
{
TT[x] = y;
}
int main()
{
in >> N >> M;
for(int i = 1; i <= N; ++i)
TT[i] = i;
while(M--)
{
int op,x,y;
in >> op >> x >> y;
if(op == 1)
Unite(Root(x),Root(y));
if(op == 2)
if(Root(x) == Root(y))
out << "DA\n";
else
out << "NU\n";
}
return 0;
}
