Pagini recente » Cod sursa (job #1365261) | Cod sursa (job #1360465) | Cod sursa (job #854642) | Cod sursa (job #272551) | Cod sursa (job #1755428)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#define dim 100009
using namespace std;
ofstream out("disjoint.out");
ifstream in("disjoint.in");
int n,m,T[dim];
void read();
void solve();
int find(int x)
{
if(x != T[x])
return T[x] = find(T[x]);
return x;
}
void unire(int x, int y)
{
int m1 = find(x);
int m2 = find(y);
T[m2] = m1;
}
int main()
{
read();
solve();
return 0;
}
void read()
{
in >> n >> m;
for(int i = 1 ; i<=n; ++i)
T[i] = i;
}
void solve()
{
for(int i = 1; i<=m; ++i)
{
int tip, x, y;
in >> tip >> x >> y;
if(tip == 1)
{
unire(x,y);
}
else
{
if(find(x) == find(y))
{
out << "DA" << '\n';
}
else
{
out << "NU" << '\n';
}
}
}
}