Pagini recente » Cod sursa (job #2071602) | Cod sursa (job #3278003) | Cod sursa (job #1447371) | Cod sursa (job #2161702) | Cod sursa (job #3266184)
#include <fstream>
using namespace std;
int t[100001];
int r[100001];
int z;
int radacina(int k)
{
if(t[k] == 0)
{
return k;
}
else
{
z = radacina(t[k]);
t[k] = z;
return z;
}
}
void unire(int p, int u)
{
int rp = radacina(p);
int ru = radacina(u);
if(r[rp] > r[ru])
{
t[ru] = rp;
}
else if(r[rp] < r[ru])
{
t[rp] = ru;
}
else
{
t[rp] = ru;
r[u]++;
}
}
int main()
{
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, m;
f>>n>>m;
for(int i = 1; i<=n ;i++)
{
t[i] = 0;
r[i] = 1;
}
int cod, x, y;
for(int i = 1; i<=m; i++)
{
f>>cod>>x>>y;
if(cod == 2)
{
if(radacina(x) == radacina(y))
{
g<<"DA"<<endl;
}
else
{
g<<"NU"<<endl;
}
}
else if(cod == 1)
{
unire(x, y);
}
}
return 0;
}