Pagini recente » Rating candale andrei (andrei93) | Cod sursa (job #2854619) | Istoria paginii utilizator/zanarok | Cod sursa (job #3212946) | Cod sursa (job #1124964)
/*VASS PETER LTC 2014-02-26*/
#include <fstream>
#include <vector>
using namespace std;
struct element{int r,p;};
vector<element> v;
void m(int i){
v[i].r=0;
v[i].p=i;
}
int f(int i){
if(v[i].p!=i)
v[i].p=f(v[i].p);
return v[i].p;
}
void u(int i,int j){
int ir=f(i);
int jr=f(j);
if(ir<jr)
v[i].p=jr;
else if(ir>jr)
v[j].p=ir;
else{
v[j].p=ir;
v[j].r+=1;
}
}
int main(){
ifstream ifs("disjoint.in");
ofstream ofs("disjoint.out");
int N,M;
ifs>>N>>M;
v.resize(N);
for(int i=0;i<N;i++)
m(i);
for(int i=0;i<M;i++){
int x,y,o;
ifs>>o>>x>>y; x--; y--;
if(1==o)
u(x,y);
else if(2==o){
if(f(x)==f(y))
ofs<<"DA\n";
else ofs<<"NU\n";
}
}
}