Pagini recente » Cod sursa (job #2282676) | Cod sursa (job #1153229) | Cod sursa (job #39145) | Cod sursa (job #1807519) | Cod sursa (job #2162952)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int nmax=100000;
int n,m;
int h[nmax+5];
int t[nmax+5];
int FindSet(int nod)
{
while(t[nod]!=nod)
nod=t[nod];
return nod;
}
void UniteSet(int x,int y)
{
if(h[x]==h[y])
{
t[x]=y;
h[x]++;
}
else
{
if(h[x]>h[y])
t[y]=x;
else
t[x]=y;
}
}
int main()
{
for(int i=1;i<=nmax;i++)
{
h[i]=1;
t[i]=i;
}
fin>>n>>m;
while(m--)
{
int tip,x,y;
fin>>tip>>x>>y;
if(tip==1)
UniteSet(FindSet(x),FindSet(y));
else
if(FindSet(x)==FindSet(y))
fout<<"DA\n";
else
fout<<"NU\n";
}
return 0;
}