Pagini recente » Monitorul de evaluare | Cod sursa (job #468545) | Cod sursa (job #3345962) | Borderou de evaluare (job #1566292) | Cod sursa (job #2805701)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n, m, t[100001], r[100001];
void read()
{
in >> n >> m;
for(int i = 1; i <= n; ++i)
t[i] = i;
}
int root(int x)
{
if(t[x] == x)
return x;
return root(t[x]);
}
void U(int x, int y)
{
if(r[x] < r[y]) t[x] = y;
else if(r[x] > r[y]) t[y] = x;
else t[y] = x, r[x]++;
}
void afis()
{
int cod, x, y;
while(m--)
{
in >> cod >> x >> y;
if(cod == 1) U(root(x), root(y));
else out << (root(x) == root(y) ? "DA\n" : "NU\n");
}
}
int main()
{
read();
afis();
return 0;
}