Pagini recente » Cod sursa (job #2122980) | Cod sursa (job #866314) | Cod sursa (job #2573220) | Cod sursa (job #1658075) | Cod sursa (job #1098610)
#include <cstdio>
using namespace std;
#define NMAX 100015
int N;
int Father[NMAX];
int Depth[NMAX];
int Die_Wurzel(int node)
{
if(node == Father[node])
return node;
return Die_Wurzel(Father[node]);
}
void Vereinen(int x, int y)
{
int fatherX = Die_Wurzel(x);
int fatherY = Die_Wurzel(y);
if(Depth[fatherX] >= Depth[fatherY])
Father[fatherY] = fatherX;
else
Father[fatherX] = fatherY;
if(Depth[fatherX] == Depth[fatherY])
Depth[fatherX]++;
}
inline bool SindBruder(int x, int y)
{
return Die_Wurzel(x) == Die_Wurzel(y);
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int M,op,x,y;
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)
Father[i] = i;
while(M--)
{
scanf("%d%d%d",&op,&x,&y);
if(op==1)
Vereinen(x,y);
else
if(SindBruder(x,y))
printf("DA\n");
else
printf("NU\n");
}
return 0;
}