Pagini recente » Cod sursa (job #2766785) | Cod sursa (job #1471822) | Cod sursa (job #2769653) | Cod sursa (job #2391139) | Cod sursa (job #2075138)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int t[100051],n,m,h[100051];
int rad(int x)
{
int r=x;
while (t[r]!=r)
{
r=t[r];
}
int y=x;
while (y!=r)
{
int t1=t[y];
t[y]=r;
y=t1;
}
return y;
}
void unio(int x,int y)
{
int c;
if (h[x]<h[y])
{
t[x]=y;
c=y;
}
else
{
t[y]=x;
c=x;
}
if (h[x]==h[y])h[c]++;
}
int i,op,x,y;
int main()
{
f>>n>>m;
for (i=1;i<=n;i++)
{
t[i]=i;
h[i]=1;
}
for (i=1;i<=m;i++)
{
f>>op>>x>>y;
if (op==2)
{
if (rad(x)==rad(y))
{
g<<"DA"<<'\n';
}
else
{
g<<"NU"<<'\n';
}
}
else
{
unio(x,y);
}
}
return 0;
}