Pagini recente » Cod sursa (job #1295700) | Cod sursa (job #1112928) | Cod sursa (job #3304183) | Cod sursa (job #287642) | Cod sursa (job #2553518)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
const int nlim=100005;
int n,m,tt[nlim],rg[nlim];
void unire(int x, int y)
{
if(rg[x]<rg[y])
tt[x]=y;
if(rg[x]>rg[y])
tt[y]=x;
if(rg[x]==rg[y])
{
tt[x]=y;
rg[y]++;
}
}
int tatal(int x)
{
while(tt[x]!=x)
x=tt[x];
return x;
}
int main()
{
int cod,x,y;
f>>n>>m;
for(int i=1;i<=n;i++)
{
tt[i]=i;
rg[i]=1;
}
for(int i=1;i<=m;i++)
{
f>>cod>>x>>y;
if(cod==1)
{
int a=tatal(x);
int b=tatal(y);
if(a!=b)
unire(x,y);
}
if(cod==2)
{
int a=tatal(x);
int b=tatal(y);
if(a==b) g<<"DA"<<'\n';
else g<<"NU"<<'\n';
}
}
return 0;
}