Pagini recente » Cod sursa (job #1401324) | Cod sursa (job #3269175) | Cod sursa (job #2832563) | Cod sursa (job #1973548) | Cod sursa (job #2525981)
#include <bits/stdc++.h>
#define NM 100005
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
void read();
int n, m, x, y, t[NM], rang[NM];
short op;
int radacina(int p)
{
if(t[p] == p)
return p;
else
return (t[p]=radacina(t[p]));
}
void unite(int x, int y)
{
int rx = radacina(x), ry = radacina(y);
if(rx!=ry)
{
if(rang[rx] > rang[ry])
t[ry] = rx;
else if(rang[rx] == rang[ry])
{
t[rx] = ry;
rang[ry]++;
}
else t[rx] = ry;
}
}
int main()
{
fin >> n >> m;
for(int i=1; i<=n; i++)
{
t[i] = i;
rang[i] = 1;
}
for(int q=1; q<=m; ++q)
{
fin >> op >> x >> y;
if(op == 1)
unite(x, y);
else if(op == 2 && radacina(x) == radacina(y))
fout << "DA" << '\n';
else fout << "NU" << '\n';
}
return 0;
}