Pagini recente » Cod sursa (job #12433) | Cod sursa (job #1561763) | Cod sursa (job #12002) | Cod sursa (job #1882837) | Cod sursa (job #2864560)
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
vector <int> h(100002);
vector <int> tata(100002);
void Union(int x, int y)
{
if(h[x] == h[y])
{
tata[x] = y;
h[y]++;
}
else
{
if(h[x] < h[y])
tata[x] = y;
else
tata[y] = x;
}
}
int FindRad(int x)
{
int cx, rad;
cx = x;
while(tata[cx] != cx)
cx = tata[cx];
rad = cx;
while(tata[x] != x)
{
cx = tata[x];
tata[x] = rad;
x = cx;
}
return x;
}
int main()
{
int N, M;
int op, x, y;
in >> N >> M;
for(int i = 0 ; i <= 100002; i++)
{
h[i] = 1;
tata[i] = i;
}
for(int i = 1; i <= M; i++)
{
in >> op >> x >> y;
if(op == 1)
{
x = FindRad(x);
y = FindRad(y);
if(x != y)
Union(x, y);
}
else
{
if(FindRad(x) == FindRad(y))
out << "DA";
else
out << "NU";
out << '\n';
}
}
return 0;
}