Pagini recente » Cod sursa (job #3213996) | Cod sursa (job #1661703) | Cod sursa (job #500553) | Cod sursa (job #1086221) | Cod sursa (job #369272)
Cod sursa(job #369272)
#include<stdio.h>
using namespace std;
#define NMAX 100005
int N, M;
int parent[NMAX], rank[NMAX];
int x, y, xRoot, yRoot;
void MakeSet(int x)
{
parent[x] = x;
rank[x] = 1;
}
int Find(int x)
{
if(parent[x] == x)
return x;
else
{
parent[x] = Find(parent[x]);
return parent[x];
}
}
void Union(int x, int y)
{
xRoot = Find(x);
yRoot = Find(y);
if(rank[xRoot] > rank[yRoot])
parent[yRoot] = xRoot;
else if(rank[xRoot] < rank[yRoot])
parent[xRoot] = yRoot;
else if(xRoot != yRoot)
{
parent[xRoot] = yRoot;
rank[xRoot]++;
}
}
int main()
{ int i, type, in, sf;
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
scanf("%d %d", &N, &M);
for(i = 1; i <= N; i++)
{MakeSet(i);}
for(i = 1; i <= M; i++)
{
scanf("%d %d %d", &type, &in, &sf);
if(type == 1)
Union(in, sf);
else //if(type == 2)
{
if(Find(in) == Find(sf))
printf("DA\n");
else printf("NU\n");
}
}
return 0;
}