Pagini recente » Istoria paginii runda/concursceva1 | Istoria paginii runda/incepatori | Cod sursa (job #183777) | Istoria paginii runda/solares | Cod sursa (job #2073193)
#include <stdio.h>
#include <stdlib.h>
/// DISJUNKT HALMAZOK - http://www.infoarena.ro/problema/disjoint
#define NMAX 10000
int nodes[NMAX];
int length[NMAX];
int root(int);
void unite(int, int);
void query(int, int);
FILE * g;
int main()
{
// input
FILE * f = fopen("input.txt", "rt");
g = fopen("output.txt", "wt");
// n - number of nodes
// m - number of operations
int n, m;
fscanf(f, "%d%d", &n, &m);
for(int i = 1 ; i <= n; ++i)
{
nodes[i] = i;
length[i] = 1;
}
int num, x, y;
for(int i = 0; i < m; ++i)
{
fscanf(f, "%d %d %d", &num, &x, &y);
if(num == 1) unite(x, y);
else query(x, y);
}
printf("\n");
return 0;
}
void unite(int x, int y)
{
int rootx = root(x);
int rooty = root(y);
if(rootx != rooty)
{
if(length[rootx] > length[rooty])
{
nodes[rooty] = rootx;
length[rootx] += length[rooty];
}
else
{
nodes[rootx] = rooty;
length[rooty] += length[rootx];
}
}
}
void query(int x, int y)
{
int rootx = root(x);
int rooty = root(y);
if(rootx == rooty) fprintf(g, "DA\n");
else fprintf(g, "NU\n");
}
int root(int node)
{
int i = node;
while(i != nodes[i])
{
nodes[i] = nodes[nodes[i]];
i = nodes[i];
}
return i;
}