Pagini recente » Cod sursa (job #1028753) | Cod sursa (job #879595) | Cod sursa (job #2575743) | Cod sursa (job #2924630) | Cod sursa (job #2928070)
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int T[100005],R[100005];
int t,x,y,i,n,m;
int root (int x)
{
int answ,aux;
answ = x;
while (T[answ]!=answ)
answ = T[answ];
while (T[x]!=x)
{
aux = T[x];
T[x] = answ;
x = aux;
}
return answ;
}
void unite (int x,int y)
{
if (R[x] == R[y])
{
T[x] = y;
R[y]++;
}
else if (R[x] > R[y])
T[y] = x;
else
T[x] = y;
}
int main()
{
fin >> n >> m;
for (i=1;i<=n;i++)
{
T[i] = i;
R[i] = 1;
}
for (i=1;i<=m;i++)
{
fin >> t >> x >> y;
if (t == 1)
unite(x,y);
else
{
int rx,ry;
rx = root(x);
ry = root (y);
if (rx == ry)
fout << "DA" << '\n';
else
fout << "NU" << '\n';
}
}
return 0;
}