Pagini recente » Cod sursa (job #563388) | Cod sursa (job #858237) | Cod sursa (job #2107927) | Cod sursa (job #939698) | Cod sursa (job #2838882)
#include <iostream>
#include <fstream>
#define MAXN 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int t[MAXN];
int root(int node)
{
int x = node;
while(t[node] > 0)
node = t[node];
while(t[x] > 0)
{
int aux = t[x];
t[x] = node;
x = aux;
}
return node;
}
void join(int a, int b)
{
int rootA = root(a), rootB = root(b);
/*if(rootA == rootB)
return;*/
if(t[rootA] <= t[rootB])
{
t[rootA] += t[rootB];
t[rootB] = rootA;
}
else
{
t[rootB] += t[rootA];
t[rootA] = rootB;
}
}
int main()
{
int n, m, op, a, b;
fin >> n >> m;
for(int i = 1; i <= n; i++)
t[i] = -1;
for(int i = 1; i <= m; i++)
{
fin >> op >> a >> b;
if(op == 1)
join(a, b);
else
{
if(root(a) == root(b))
fout << "DA" << endl;
else
fout << "NU" << endl;
}
}
fin.close(); fout.close();
return 0;
}