Pagini recente » Cod sursa (job #128573) | Cod sursa (job #398526) | Cod sursa (job #1364214) | Cod sursa (job #1522794) | Cod sursa (job #3195929)
#include <bits/stdc++.h>
#define SIZE 100005
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m,tata[SIZE],dim[SIZE];
int tata_multime(int x)
{
if(x!=tata[x])tata[x]=tata_multime(tata[x]);
return tata[x];
}
void join(int x,int y)
{
x=tata_multime(x);
y=tata_multime(y);
if(dim[x]<dim[y])
{
dim[y]+=dim[x];
tata[x]=y;
}
else
{
dim[x]+=dim[y];
tata[y]=x;
}
}
int main()
{
int i,c,x,y;
f>>n>>m;
for(i=1;i<=n;i++)
{
tata[i]=i;
dim[i]=1;
}
for(i=1;i<=m;i++)
{
f>>c>>x>>y;
if(c==1)
{
join(x,y);
}
else
{
if(tata_multime(x)==tata_multime(y))
g<<"DA"<<'\n';
else g<<"NU"<<'\n';
}
}
return 0;
}