Pagini recente » Cod sursa (job #1234088) | Cod sursa (job #1593255) | Cod sursa (job #57044) | Cod sursa (job #957112) | Cod sursa (job #2862858)
#include <iostream>
#include <fstream>
#include <stdio.h>
#define MAXN 100005
using namespace std;
int t[MAXN], n;
int get_root(int x)
{
int nod = x, aux;
while(t[x] > 0)
x = t[x];
while(nod != x)
{
aux = t[nod];
t[nod] = x;
nod = aux;
}
return x;
}
void join(int x, int y)
{
int rootX = get_root(x), rootY = get_root(y);
if(rootX == rootY)
return;
if(t[rootX] < t[rootY])
{
t[rootX] += t[rootY];
t[rootY] = rootX;
}
else
{
t[rootY] += t[rootX];
t[rootX] = rootY;
}
}
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int main()
{
int i, m, op, x, y;
fin >> n >> m;
for(i = 1; i <= n; i++)
t[i] = -1;
for(i = 1; i <= m; i++)
{
fin >> op >> x >> y;
if(op == 1)
{
join(x, y);
}
else
{
if(get_root(x) == get_root(y))
fout << "DA" << endl;
else
fout << "NU" << endl;
}
}
fin.close(); fout.close();
return 0;
}