Pagini recente » Cod sursa (job #2027612) | Cod sursa (job #2846415) | Cod sursa (job #319951) | Cod sursa (job #2677949) | Cod sursa (job #2170025)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int r[100003],p[100003],n,m;
int Find(int x)
{
if(x!=p[x]) p[x]=Find(p[x]);
return p[x];
}
void Union(int x, int y)
{
int xroot,yroot;
xroot=Find(x);
yroot=Find(y);
if(r[xroot]<=r[yroot])
{
r[yroot]+=r[xroot];
p[xroot]=p[yroot];
}
else
{
r[xroot]+=r[yroot];
p[yroot]=p[xroot];
}
}
int main()
{
int i,cod,x,y;
///p[] - parent ; r[] - rank
f>>n>>m;
for(i=1;i<=n;i++)
{
p[i]=i;
r[i]=1;
}
for(i=1;i<=m;i++)
{
f>>cod>>x>>y;
if(cod==1)
{
Union(x,y);
}
else
{
if(Find(x)!=Find(y)) g<<"NU"<<'\n';
else g<<"DA"<<'\n';
}
}
f.close();
g.close();
return 0;
}