Pagini recente » Cod sursa (job #2270225) | Cod sursa (job #2151102) | Cod sursa (job #2911962) | Cod sursa (job #2578308) | Cod sursa (job #2081354)
#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;
inline void arbore1 (nod *x)
{
x->p=x;
x->gr=0;
}
inline 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++;
}
}
inline nod * radacina (nod * x)
{
if(x != x->p)
x->p=radacina(x->p);
return x->p;
}
inline 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();
}