Pagini recente » Cod sursa (job #2278786) | Cod sursa (job #2300347) | Cod sursa (job #1966189) | Cod sursa (job #2028721) | Cod sursa (job #2494685)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
const int NMAX = 1000001;
int n,m,t[NMAX],h[NMAX];
/**
int Find(int x)
{
int y,z;
for(y=t[x];y!=t[y];y=t[y]);///gasim radacina
while(x!=t[x])///facem compresia
{
z=t[x];
t[x]=y;
x=z;
}/// legam toate legaturile la radacin a
return y;
*/
/**
int Find(int x)
{
if(t[x]==x)
return x;
t[x]=Find(t[x]);
return t[x];
*/
int Find(int x)
{
if(t[x]==x)
return x;
return t[x] = Find(t[x]);
}
void Union(int x,int y)
{
if(h[x]<h[y])
t[x]=y;
else
{
t[y]=x;
if(h[x]==h[y])
h[y]++;
}
}
int main()
{
f>>n>>m;
for(int i=1;i<=n;i++)
{
t[i]=i;
h[i]=1;
}
while(m--)
{
int cod,x,y;
f>>cod>>x>>y;
x=Find(x);
y=Find(y);
if(cod==1)
//if(x!=y)
Union(x,y);
else
if(x==y)
g<<"DA"<<'\n';
else
g<<"NU"<<'\n';
}
return 0;
}