Pagini recente » Cod sursa (job #2104030) | Cod sursa (job #2312263) | Cod sursa (job #127752) | Cod sursa (job #415008) | Cod sursa (job #2439950)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int nmax = 100001;
int t[nmax], c[nmax], n, m;
void Union(int x, int y)
{
c[x] += c[y];
t[x] = y;
}
inline int Find(int x)
{
int rad = x, y;
while(t[rad] > 0)
{
rad = t[rad];
}
while(t[x] > 0)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; ++i)
{
int cod, x, y;
fin >> cod >> x >> y;
if(cod == 1)
{
int r1 = Find(x);
int r2 = Find(y);
if(r1 != r2)
Union(x, y);
}
else
{
int r1 = Find(x);
int r2 = Find(y);
if(r1 == r2)
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}