Pagini recente » Cod sursa (job #314993) | Cod sursa (job #222222) | Cod sursa (job #377999) | Cod sursa (job #919180) | Cod sursa (job #3141998)
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n, m;
vector<int> f;
void unite(int x, int y){
if(!f[x]){
f[x] = y;
return;
}
unite(f[x], y);
f[x] = f[f[x]];
}
int supreme_father(int x){
if(!f[x])
return x;
f[x] = supreme_father(f[x]);
return f[x];
}
int main(){
cin>>n>>m;
f.resize(n + 2);
while(m--){
int tip, x, y;
cin>>tip>>x>>y;
if(tip == 1)
unite(x, y);
else{
if(supreme_father(x) == supreme_father(y))
cout<<"DA\n";
else cout<<"NU\n";
}
}
return 0;
}