Pagini recente » Cod sursa (job #1190714) | Cod sursa (job #1888656) | Cod sursa (job #493044) | Cod sursa (job #1473676) | Cod sursa (job #2010770)
#include <fstream>
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()
{
ofstream fout ("disjoint.out");
ifstream fin ("disjoint.in");
fin >> n >> m;
for (i=1; i<=n; ++i)
{
tata[i] = i;
card[i] = 1;
}
for (i=0; i<m; ++i)
{
fin >> verif >> x >> y;
if (verif == 1)
unire(x, y);
if (verif == 2)
{
if (stapan(x) == stapan(y))
fout << "DA\n";
else fout << "NU\n";
}
}
return 0;
}