Pagini recente » Cod sursa (job #975808) | Cod sursa (job #465167) | Cod sursa (job #550998) | Cod sursa (job #498338) | Cod sursa (job #2572105)
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int N = 1e5 + 5;
int TT[N]; ///vectorul de tati
int RG[N]; ///rangul(inaltimea) subarborelui in i
int n, m, i, task, x, y;
int find_root(int node)
{
int aux = node;
while(TT[aux] != 0)
aux = TT[aux];
return aux;
}
void unite(int node_x, int node_y)
{
node_x = find_root(node_x);
node_y = find_root(node_y);
if(RG[node_x] < RG[node_y])
swap(node_x, node_y);
TT[node_y] = node_x;
RG[node_x] += RG[node_y];
}
main()
{
in >> n >> m;
while(m--)
{
in >> task >> x >> y;
if(task == 1)
{
unite(x,y);
}
else
{
if(find_root(x) != find_root(y))
out << "NU\n";
else
out<<"DA\n";
}
}
}