Pagini recente » Cod sursa (job #3031593) | Cod sursa (job #1330124) | Cod sursa (job #509908) | Cod sursa (job #2632081) | Cod sursa (job #2838877)
#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)
{
if(t[node] < 0)
return node;
return t[node] = root(t[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";
else
fout << "NU";
}
}
fin.close(); fout.close();
return 0;
}