Pagini recente » Cod sursa (job #2539200) | Cod sursa (job #116151) | Cod sursa (job #3289585) | Cod sursa (job #1813933) | Cod sursa (job #2728662)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, x, y, z, sef[100005], rang[100005];
int find(int sef[], int i)
{
if (sef[i] != i)
sef[i] = find(sef, sef[i]);
return sef[i];
}
void unite(int sef[], int x, int y)
{
int xx = find(sef, x);
int yy = find(sef, y);
if (rang[xx] > rang[yy])
sef[yy] = xx;
else
sef[xx] = yy;
if (rang[xx] == rang[yy])
rang[yy] ++;
}
int main() {
fin >> n >> m;
for (int i=1;i<=n;i++){
sef[i] = i;
rang[i] = 1;
}
for (int i=1;i<=m;i++){
fin >> x >> y >> z;
if (x == 2){
if (find(sef, y) == find(sef, z)){
fout << "DA\n";
}else{
fout << "NU\n";
}
}else{
unite(sef, y, z);
}
}
return 0;
}