Pagini recente » Statistici Voicu Latis (vlatis) | Monitorul de evaluare | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #2437040)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX = 100020;
int R[NMAX], T[NMAX];
int FIND(int x)
{
int R;
for(R = x; R != T[R]; R = T[R]);
int y;
while(x != T[x])
{
y = T[x];
T[x] = R;
x = y;
}
return R;
}
int UNION(int x, int y)
{
if(R[x] > R[y])
{
T[y] = x;
}
else
{
T[x] = y;
}
if(R[x] == R[y])
{
R[y]++;
}
}
int main()
{
int n, m;
fin>>n>>m;
int cod, i, x, y;
for(i=1; i<=n; i++)
{
R[i] = 1;
T[i] = i;
}
for(i=1; i<=m; i++)
{
fin>>cod>>x>>y;
if(cod == 1)
{
if(FIND(x) != FIND(y))
UNION(FIND(x), FIND(y));
}
else
{
if(FIND(x) == FIND(y))
{
fout<<"DA\n";
}
else
{
fout<<"NU\n";
}
}
}
fin.close();
fout.close();
return 0;
}