Pagini recente » Cod sursa (job #339061) | Cod sursa (job #1314786) | Cod sursa (job #2467560) | Cod sursa (job #1591880) | Cod sursa (job #1888929)
#include <fstream>
#define nmax 100001
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int t[nmax],h[nmax],n,m;
int Find(int x)
{
while(t[x]) x=t[x];
return x;
}
void Union(int x,int y)
{
if(h[x]>h[y]) t[y]=x;
else
{ t[x]=y;
if(h[x]==h[y]) h[y]++;
}
}
int main()
{ int i,c,x,y;
fin>>n>>m;
for(i=1;i<=m;i++)
{ fin>>c>>x>>y;
if(c==1)
{ x=Find(x); y=Find(y);
if(x!=y) Union(x,y);
}
else
{ x=Find(x); y=Find(y);
if(x==y) fout<<"DA"<<"\n";
else fout<<"NU"<<"\n";
}
}
return 0;
}