Pagini recente » Cod sursa (job #1063802) | Cod sursa (job #1533872) | Cod sursa (job #1927866) | Cod sursa (job #2950881) | Cod sursa (job #3258168)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout("disjoint.out");
int n, m, c, x, y;
const int Max = 1e5 + 1;
vector<int> parent(Max, -1), d(Max, 1);
int find_set(int x)
{
if(parent[x] == -1)
return x;
return parent[x] = find_set(parent[x]);
}
void union_sets(int x, int y)
{
x = find_set(x);
y = find_set(y);
if(x == y)
return;
if(d[x] < d[y])
swap(x, y);
parent[y] = x;
d[x] += d[y];
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; ++i)
{
fin >> c >> x >> y;
if(c == 1)
union_sets(x, y);
else if(find_set(x) == find_set(y))
fout << "DA\n";
else
fout << "NU\n";
}
fin.close();
fout.close();
return 0;
}