Pagini recente » Cod sursa (job #2841474) | Cod sursa (job #1396841) | Cod sursa (job #1843995) | Cod sursa (job #1844000) | Cod sursa (job #2948835)
#include<fstream>
#include<vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
vector<int> t, h;
int radacina(int x)
{
while(x != t[x])
{
x = t[x];
}
return x;
}
void unifica(int x, int y)
{
//x si y sunt radacinile arborilor care se unifica
if(h[x] > h[y])
{
t[y] = x;
}
else
{
if(h[y] > h[x])
{
t[x] = y;
}
else
{
h[x]++;
t[y] = x;
}
}
}
int main()
{
int n, m, c, x, y, i, rx, ry;
fin>>n>>m;
t.assign(n + 1, 0);
h.assign(n + 1, 0);
for(i = 1; i <= n; ++i)
{
t[i] = i;
}
for(i = 1; i <= m; ++i)
{
fin>>c>>x>>y;
rx = radacina(x);
ry = radacina(y);
if(c == 1)
{
if(rx != ry)
unifica(rx, ry);
}
else
{
if(rx == ry)
{
fout<<"DA"<<"\n";
}
else
{
fout<<"NU"<<"\n";
}
}
}
fin.close();
fout.close();
return 0;
}