Pagini recente » Cod sursa (job #754306) | Cod sursa (job #2731640) | Cod sursa (job #1003066) | Cod sursa (job #1058129) | Cod sursa (job #2079145)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
struct nod
{
int gr;
nod *p;
};
nod *v[100050];
int n, m;
void arbore1 (nod *x)
{
x->p=x;
x->gr=0;
}
void link (nod * x, nod * y)
{
if(x->gr > y->gr)
y->p=x;
else
{ x->p=y;
if(x->gr == y->gr)
y->gr++;
}
}
nod * radacina (nod * x)
{
if(x != x->p)
x->p=radacina(x->p);
return x->p;
}
void unire(nod * x, nod * y)
{
link(radacina(x),radacina(y));
}
void citire ()
{
f>>n>>m;
int i, x, y, op;
for(i=1;i<=n;++i)
v[i]=new(nod),arbore1(v[i]);
while(m--)
{
f>>op>>x>>y;
if(op==1)
unire(v[x], v[y]);
else
if(radacina(v[x])==radacina(v[y])) g<<"DA"<<endl;
else g<<"NU"<<endl;
}
f.close();
g.close();
}
int main()
{
citire();
}