Pagini recente » Monitorul de evaluare | Cod sursa (job #2679149) | Cod sursa (job #3303263) | Cod sursa (job #3341926) | Cod sursa (job #3317614)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, t, x, y, sef[100001];
int boss(int x){
if(sef[x] == x){
return x;
}
else{
return sef[x] = boss(sef[x]);
}
}
void unite(int x, int y){
int sefx = boss(x), sefy = boss(y);
sef[sefy] = sefx;
}
bool together(int x, int y){
return boss(x) == boss(y);
}
void read(){
fin >> n >> m;
for(int i = 1; i <= n; i++){
sef[i] = i;
}
for(int i = 1; i <= m; i++){
fin >> t >> x >> y;
if(t == 1){
unite(x, y);
}
else{
if(together(x, y)){
fout << "DA\n";
}
else{
fout << "NU\n";
}
}
}
}
int main()
{
read();
return 0;
}