Pagini recente » Cod sursa (job #2012770) | Cod sursa (job #1570892) | Istoria paginii runda/ciuscatalin/clasament | Monitorul de evaluare | Cod sursa (job #2741539)
#include <bits/stdc++.h>
#define NMAX 100009
#define ll long long int
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int tata[NMAX];
int h[NMAX];
int n, m;
int Find(int x);
void Union(int x, int y);
int main()
{
int i;
int x, y, cer;
fin >> n >> m;
for (i = 1; i <= m; i++)
{
fin >> cer;
fin >> x >> y;
if (cer == 1)
{
int cx = Find(x);
int cy = Find(y);
if(cx!=cy)
Union(cx, cy);
}
else
{
int cx = Find(x);
int cy = Find(y);
if (cx == cy)
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}
int Find(int x)
{
int copx = x;
while (tata[x])
{
x = tata[x];
}
int rad = x;
x= copx;//refac
while (tata[x] )
{
copx = tata[x];
tata[x] = rad;///leg direct
x = copx;
}
return rad;
}
void Union(int x, int y)
{
if (h[x] > h[y])///daca lipesc e ok
tata[y] = x;
else
{
tata[x] = y;
if (h[x] == h[y])
h[y]++;
}
}