Pagini recente » Cod sursa (job #2784855) | Cod sursa (job #2543330) | Cod sursa (job #2633329) | Cod sursa (job #1054316) | Cod sursa (job #1613036)
#include <stdio.h>
#include <stdlib.h>
struct node{
unsigned long i;
node *left,*right;
}**v;
void VSD(node *v){
if(v!=NULL){
printf("%lu ",v->i);
if(v->left)
VSD(v->left);
if(v->right)
VSD(v->right);
}
}
void add(node *&x,node *y){
if(x!=NULL){
if(y->i<x->i)
add(x->left,y);
else
add(x->right,y);
}else
x=y;
}
const char* Search(node *x,unsigned long y){
while(x!=NULL)
if(x->i==y)
return "DA";
else if(y<x->i)
x=x->left;
else
x=x->right;
return "NU";
}
void change(unsigned long &x,unsigned long &y){
unsigned long man=x;
x=y;
y=man;
}
int main(){
unsigned long n,m;
FILE*f=fopen("disjoint.in","r");
fscanf(f,"%lu %lu",&n,&m);
v=(node**)malloc(sizeof(node)*n);
for(unsigned long i=0;i<n;i++){
v[i]=(node*)malloc(sizeof(node));
v[i]->left=NULL;
v[i]->right=NULL;
v[i]->i=i;
}
FILE*g=fopen("disjoint.out","w");
while(m--){
short op;
unsigned long x,y;
fscanf(f,"%hd %lu %lu",&op,&x,&y);
if(x>y)
change(x,y);
if(op==1)
add(v[x-1],v[y-1]);
else
fprintf(g,"%s\n",Search(v[x-1],y-1));
}
for(unsigned long i=0;i<n;i++){
VSD(v[i]);
printf("\n");
}
fclose(f);
fclose(g);
return 0;
}