Pagini recente » Cod sursa (job #2307681) | Cod sursa (job #1015112) | Cod sursa (job #262823)
Cod sursa(job #262823)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
#define NMAX 100005
int P[NMAX], S[NMAX], N, M;
int findNod (int x)
{
int R, y;
for (R = x; P[R] != R; R = P[R]); //Cautare Radacina
for (; x != R;) //Compresie Arbore
{
y = P[x];
P[x] = R;
x = y;
}
return R;
}
void mergeTr (int x, int y)
{
if (S[x] > S[y]) P[y] = x;
else P[x] = y;
if (S[x] == S[y])
S[y] ++;
}
int main()
{
int p, x, y, i;
fin >>N >>M;
for (i = 1; i <= N; i++)
P[i] = i;
for (i = 1; i <= M; i++)
{
fin >>p >>x >>y;
x = findNod(x);
y = findNod(y);
if (p == 2)
if (x == y) fout <<"DA\n"; else fout<<"NU\n";
if (p == 1)
mergeTr(x,y);
}
fout.close();
return 0;
}