Pagini recente » Cod sursa (job #2352461) | Cod sursa (job #2244619) | Cod sursa (job #2770008) | Cod sursa (job #596708) | Cod sursa (job #2058649)
#include <fstream>
#define Nmax 100002
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int t[Nmax],n,m;
int h[Nmax];
int Find(int x)
{
while(t[x]!=0) x=t[x];
return x;
}
void Union(int x,int y)
{
if(h[x]>h[y]) t[y]=x;
else if(h[x]<h[y]) t[x]=y;
else
{
t[x]=y; h[y]++;
}
}
int main()
{
fin>>n>>m;
int i,x,y,cod,c1,c2;
for(i=1;i<=m;i++)
{
fin>>cod>>x>>y;
c1=Find(x); c2=Find(y);
if(cod==1&&c1!=c2) Union(c1,c2);
else if(cod==2){if(c1==c2) fout<<"DA\n"; else fout<<"NU\n";}
}
return 0;
}