Pagini recente » Cod sursa (job #2834197) | Cod sursa (job #353026) | Cod sursa (job #2890440) | Cod sursa (job #2437249) | Cod sursa (job #1039200)
#include<fstream>
using namespace std;
int t[100005],h[100005];
int findset(int x)
{
while(t[x]!=x)
x=t[x];
return x;
}
void unionset(int x,int y)
{
if(h[x]==h[y])
{
h[x]++;
t[y]=x;
}
else
if(h[x]<h[y])
t[x]=y;
else
t[y]=x;
}
int main()
{
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n,m,x,y,i,j,a;
in>>n>>m;
for(i=1;i<=n;i++)
t[i]=i;
for(i=1;i<=m;i++){
in>>a>>x>>y;
if(a==1) unionset(findset(x),findset(y));
else if(a==2)
if(findset(x)==findset(y)) out<<"DA\n";
else out<<"NU\n";
}
return 0;
}