Pagini recente » Borderou de evaluare (job #702405) | Borderou de evaluare (job #1337549) | Cod sursa (job #3171086) | Borderou de evaluare (job #2106857) | Cod sursa (job #1800673)
#include <bits/stdc++.h>
using namespace std;
int tata[100001], n, i, j, card[100001], m, verif, x, y;
int stapan (int nod)
{
int rege = nod, copie;
while (tata[rege] != rege)
rege = tata[rege];
while (tata[nod] != nod)
{
copie = nod;
nod = tata[nod];
tata[copie] = rege;
}
return rege;
}
void unire (int a, int b)
{
int st1 = stapan(a), st2 = stapan(b);
if (st2 == st1)
return ;
if (card[st1] > card[st2])
{
card[st1]+=card[st2];
tata[st2] = st1;
}
else
{
card[st2] += card[st1];
tata[st1] = st2;
}
}
int main()
{
freopen ("disjoint.in", "r", stdin);
freopen ("disjoint.out", "w", stdout);
cin >> n >> m;
for (i=1; i<=n; ++i)
{
tata[i] = i;
card[i] = 1;
}
for (i=0; i<m; ++i)
{
cin >> verif >> x >> y;
if (verif == 1)
unire(x, y);
if (verif == 2)
{
if (stapan(x) == stapan(y))
cout << "DA\n";
else cout << "NU\n";
}
}
return 0;
}