Pagini recente » Cod sursa (job #1916714) | Cod sursa (job #2563695) | Cod sursa (job #1156937) | Cod sursa (job #104024) | Cod sursa (job #2375843)
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N, M;
int TT[Nmax], RG[Nmax];
int c,x,y;
int Find(int Nod)
{
int R, y;
R = Nod;
while (TT[Nod] != Nod) //Cautarea stramosului
{
Nod = TT[Nod];
}
while (TT[R] != R) //Compresia arborelui
{
y = TT[R];
TT[R] = Nod;
R = y;
}
return Nod;
}
void Unire(int x, int y)
{
if (RG[x] > RG[y])
TT[y] = x;
else
TT[x] = y;
if (RG[x] == RG[y])
RG[y]++;
}
int main()
{
fin >> N >> M;
for (int i=1; i<N; i++)
{
TT[i] = i;
RG[i] = 1;
}
for (int i=1; i<=M; i++)
{
fin >> c >> x >> y;
if (c == 2)
{
if (Find(x) == Find(y))
fout << "DA\n";
else
fout << "NU\n";
}
else
{
int tata_x = Find(x);
int tata_y = Find(y);
if (tata_x == tata_y)
{
// se spune in problema ca tata_x != tata_y
}
Unire(tata_x, tata_y);
}
}
return 0;
}