Pagini recente » Cod sursa (job #375982) | Cod sursa (job #3030920) | Cod sursa (job #130665) | Cod sursa (job #1978791) | Cod sursa (job #2651363)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m;
int tata[100100];
int dimensiune[100100];
int tata_multime(int x)
{
if(x != tata[x])
return tata_multime(tata[x]);
return tata[x];
}
int unire(int x, int y)
{
x = tata_multime(x);
y = tata_multime(y);
if(dimensiune[x] >= dimensiune[y])
{
dimensiune[x] += dimensiune[y];
tata[y] = x;
}
else
{
dimensiune[y] += dimensiune[x];
tata[x] = y;
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= n; i ++)
{
tata[i] = i;
dimensiune[i] = 1;
}
for(int i = 1; i <= m; i ++)
{
int operatie, x, y;
fin >> operatie;
fin >> x >> y;
if(operatie == 1)
unire(x,y);
else
{
if(tata_multime(x) == tata_multime(y))
fout << "DA" << '\n';
else
fout << "NU" << '\n';
}
}
}