Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int *red, p=0, k=0;
- typedef struct cev{
- struct lista_cev *lista;
- struct cev *roditelj;
- char stanje;
- char ind;
- }cev;
- typedef struct lista_cev{
- int vrednost;
- struct lista_cev *sledeci;
- }lista_cev;
- void ubaci(cev *a, int b){
- lista_cev *pom;
- if((a->lista)==NULL){
- a->lista=malloc(sizeof(lista_cev));
- a->lista->vrednost=b;
- return;
- }
- pom=a->lista;
- while(1){
- if((pom->sledeci)==NULL){
- pom->sledeci=malloc(sizeof(lista_cev));
- pom->vrednost=b;
- return;
- }
- pom=pom->sledeci;
- }
- }
- void napraviStablo(cev *cevi){
- while(p<=k){
- lista_cev *pom=cevi[p].lista;
- while(pom!=NULL){
- if(cevi[pom->vrednost].ind){
- pom=pom->sledeci;
- continue;
- }
- k++;
- red[k]=(pom->vrednost);
- cevi[pom->vrednost].roditelj=cevi+p;
- cevi[pom->vrednost].ind=1;
- pom=pom->sledeci;
- }
- p++;
- }
- }
- void nadji(cev *cevi){
- int n, a, i, k;
- cev *pom;
- scanf("%d", &n);
- for(i=0; i<n; i++){
- scanf("%d", &a);
- a--;
- pom=cevi+a;
- k=0;
- while(1){
- if((pom->stanje)=='T'){
- printf("%d\n", k);
- break;
- }
- pom->stanje='T';
- pom=pom->roditelj;
- k++;
- }
- }
- }
- int main(){
- cev *cevi;
- int n, a, b, i;
- scanf("%d", &n);
- cevi=malloc(n*sizeof(cev));
- red=malloc(n*sizeof(int));
- red[0]=0;
- cevi[0].stanje='T';
- cevi[0].ind=1;
- for(i=1; i<n; i++){
- cevi[i].stanje='H';
- cevi[i].ind=0;
- }
- for(i=0; i<n-1; i++){
- scanf("%d%d", &a, &b);
- ubaci(cevi+(a-1), b-1);
- ubaci(cevi+(b-1), a-1);
- }
- napraviStablo(cevi);
- nadji(cevi);
- return 0;
- }
- //roditelji u pogresnom redosledu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement