Pagini recente » Borderou de evaluare (job #245764) | Borderou de evaluare (job #244788) | Cod sursa (job #1886309)
#include <iostream>
#include <fstream>
#define nmax 100000+5
using namespace std;
int n, m, cod;
int tata[nmax];
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int fnd(int x)
{
int r=x;
while(tata[r]) r=tata[r];
int y=x, aux;
while(y!=r)
{
aux=tata[y]; tata[y]=r; y=aux;
}
return r;
}
int main()
{
int c1, c2;
f >> n >> m;
for(int i=0, x, y; i<m; ++i)
{
f >> cod >> x >> y;
c1 = fnd(x); c2=fnd(y);
if(cod == 1)
tata[c1] = c2;
else
{
if(c1 == c2)
g << "DA" << '\n';
else
g << "NU" << '\n';
}
}
f.close();
g.close();
return 0;
}