Pagini recente » Cod sursa (job #1091282) | Cod sursa (job #1239630) | Istoria paginii runda/hjghj/clasament | Istoria paginii runda/concurs102.3654 | Cod sursa (job #484959)
Cod sursa(job #484959)
#include<fstream.h>
const int NMAX=100005;
int T[NMAX],n,m,RG[NMAX];
int find(int x)
{int R,y;
for(R=x;R!=T[R];R=T[R]);
for(;x!=T[x];)
{y=T[x];
T[x]=R;
x=y;
}
return R;
}
void unite(int x,int y)
{ if(RG[x]>RG[y])
T[y]=x;
else
T[x]=y;
if(RG[x]==RG[y])
RG[y]++;
}
int main()
{ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
fin>>n>>m;
int i,op,x,y;
for(i=1;i<=n;++i)
{T[i]=i;RG[i]=1;}
for(i=1;i<=m;++i)
{fin>>op>>x>>y;
if(op==2)
{if(find(x)==find(y))
fout<<"DA"<<'\n';
else
fout<<"NU\n";
}
else
{if(find(x)==find(y))
;
unite(find(x),find(y));
}
}
fin.close();
fout.close();
return 0;
}