Pagini recente » Cod sursa (job #1235633) | Cod sursa (job #1388529) | Rating Cirnu Bianca Georgiana (Bianca_Cirnu_) | Cod sursa (job #1593315) | Cod sursa (job #1408527)
#include <iostream>
#include <fstream>
#include <cstdio>
#define nmax 100005
using namespace std;
FILE *fi, *fo;
int n, m;
int tip, a, b;
int T[nmax], NR[nmax];
int root(int nod)
{
if (T[nod] == nod)
return nod;
return root(T[nod]);
}
void uneste(int x, int y)
{
int p1 = root(x);
int p2 = root(y);
if (p1 == p2)
return;
if (NR[p1] >= NR[p2])
{
NR[p1] += NR[p2];
T[p2] = p1;
}
else
{
NR[p2] += NR[p1];
T[p1] = p2;
}
}
string checkRoot(int x, int y)
{
int p1 = root(x);
int p2 = root(y);
if (p1 == p2)
return "DA";
return "NU";
}
int main()
{
fi = fopen("disjoint.in", "r");
fo = fopen("disjoint.out", "w");
fscanf(fi, "%d%d", &n, &m);
for (int i = 1; i <= n; i++)
T[i] = i, NR[i] = 1;
for (int i = 1; i <= m; i++)
{
fscanf(fi, "%d%d%d", &tip, &a, &b);
switch (tip) {
case 1:
uneste(a, b);
break;
case 2:
fprintf(fo, "%s\n", checkRoot(a, b).c_str());
break;
}
}
fclose(fi);
fclose(fo);
return 0;
}