Pagini recente » Cod sursa (job #2325484) | Cod sursa (job #3148345) | Cod sursa (job #1256798) | Cod sursa (job #2486853) | Cod sursa (job #2414498)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m,x,y,c,i,j,ca[100010],t[100010],root(int);
int main()
{
f>>n>>m;
for(i=1;i<=n;i++)t[i]=i;
for(;m;m--)
{
f>>c>>x>>y;
x=root(x);
y=root(y);
if(c==1)
{
if(ca[x]>ca[y])
t[y]=x;
else
if(ca[x]<ca[y])
t[x]=y;
else
t[y]=x,ca[x]++;
}
else
x==y?g<<"DA\n":g<<"NU\n";
}
return 0;
}
int root(int nod)
{
if(t[nod]==nod)return nod;
return t[nod]=root(t[nod]);
}