Pagini recente » Cod sursa (job #2519674) | Cod sursa (job #3040000) | Cod sursa (job #385703) | Cod sursa (job #1588277) | Cod sursa (job #2350236)
#include <bits/stdc++.h>
#define NUM 100005
using namespace std;
int pt[NUM];
int rg[NUM];
int n, m, cod, x, y;
int cauta(int x)
{
int nod, aux;
// caut nodul radacina
for(nod = x; nod != pt[nod]; nod = pt[nod]);
// fiecare nod se leaga de radacina, pe parcurs
while(x != pt[x])
{
aux = pt[x];
pt[x] = nod;
x = aux;
}
return nod;
}
void unite(int x, int y)
{
if(rg[x] > rg[y])
pt[y] = x;
else
pt[x] = y;
if(rg[x] == rg[y])
rg[x]++;
}
int main()
{
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f >> n >> m;
for(int i = 1; i <= n; ++i)
{
pt[i] = i;
rg[i] = 1;
}
for(int i = 1; i <= m; ++i)
{
f >> cod >> x >> y;
if(cod == 1)
{
unite(cauta(x), cauta(y));
}
else
{
if(cauta(x) == cauta(y))
g << "DA\n";
else
g << "NU\n";
}
}
f.close();
g.close();
return 0;
}