Pagini recente » Cod sursa (job #2432835) | Cod sursa (job #1580821) | Cod sursa (job #2124560) | Cod sursa (job #1366316) | Cod sursa (job #2460486)
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m,i,tip,x,y,xroot,yroot,tata[100020],Rank[100020];
int cauta(int x)
{
int R, y;
//merg in sus pe arbore pana gasesc un nod care pointeaza catre el insusi
for (R = x; tata[R] != R; R = tata[R]);
//aplic compresia drumurilor
for (; tata[x] != x;)
{
y = tata[x];
tata[x] = R;
x = y;
}
return R;
}
void Union(int x,int y)
{
if(Rank[x] > Rank[y])
tata[y] = x;
else if(Rank[x] < Rank[y])
tata[x] = y;
else
{
tata[y] = x;
++ Rank[x];
}
}
int main()
{
f >> n >> m;
for(i = 1; i <= n; ++ i)
tata[i] = i;
while(m --)
{
f >> tip >> x >> y;
xroot = cauta(x);
yroot = cauta(y);
if(tip == 1)
{
if(x != y)
Union(x,y);
}
else
{
if(xroot == yroot)
g << "DA" << '\n';
else
g << "NU" << '\n';
}
}
}