Pagini recente » Cod sursa (job #3228320) | Cod sursa (job #2906517) | Cod sursa (job #2617382) | Cod sursa (job #884660) | Cod sursa (job #2795892)
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int lim=1e5+4;
int link[lim],dim[lim];
int n,m,t,x,y;
int tata(int x)
{
int cpy=x,aux;
while(x!=link[x]) x=link[x];
while(cpy!=link[cpy]) aux=cpy,cpy=link[cpy],link[aux]=x;
return x;
}
void unite(int x,int y)
{
x=tata(x);
y=tata(y);
if(dim[x]<dim[y])
swap(x,y);
dim[x]+=dim[y];
link[y]=x;
}
int main()
{
in>>n>>m;
for(int i=1;i<=n;++i)
link[i]=i,dim[i]=1;
for(int i=1;i<=m;++i)
{
in>>t>>x>>y;
if(t==1) unite(x,y);
else
{
if(tata(x)==tata(y))
out<<"DA"<<'\n';
else out<<"NU"<<'\n';
}
}
return 0;
}