Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int *red, p=0, k=0;
  5. typedef struct cev{
  6. struct lista_cev *lista;
  7. struct cev *roditelj;
  8. char stanje;
  9. char ind;
  10. }cev;
  11.  
  12. typedef struct lista_cev{
  13. int vrednost;
  14. struct lista_cev *sledeci;
  15. }lista_cev;
  16.  
  17. void ubaci(cev *a, int b){
  18. lista_cev *pom;
  19. if((a->lista)==NULL){
  20. a->lista=malloc(sizeof(lista_cev));
  21. a->lista->vrednost=b;
  22. return;
  23. }
  24. pom=a->lista;
  25. while(1){
  26. if((pom->sledeci)==NULL){
  27. pom->sledeci=malloc(sizeof(lista_cev));
  28. pom->vrednost=b;
  29. return;
  30. }
  31. pom=pom->sledeci;
  32. }
  33.  
  34. }
  35. void napraviStablo(cev *cevi){
  36. while(p<=k){
  37. lista_cev *pom=cevi[p].lista;
  38. while(pom!=NULL){
  39. if(cevi[pom->vrednost].ind){
  40. pom=pom->sledeci;
  41. continue;
  42. }
  43.  
  44. k++;
  45. red[k]=(pom->vrednost);
  46. cevi[pom->vrednost].roditelj=cevi+p;
  47. cevi[pom->vrednost].ind=1;
  48. pom=pom->sledeci;
  49. }
  50. p++;
  51. }
  52. }
  53. void nadji(cev *cevi){
  54. int n, a, i, k;
  55. cev *pom;
  56. scanf("%d", &n);
  57. for(i=0; i<n; i++){
  58. scanf("%d", &a);
  59. a--;
  60. pom=cevi+a;
  61. k=0;
  62. while(1){
  63. if((pom->stanje)=='T'){
  64. printf("%d\n", k);
  65. break;
  66. }
  67. pom->stanje='T';
  68. pom=pom->roditelj;
  69. k++;
  70. }
  71.  
  72.  
  73.  
  74. }
  75. }
  76. int main(){
  77. cev *cevi;
  78. int n, a, b, i;
  79. scanf("%d", &n);
  80. cevi=malloc(n*sizeof(cev));
  81. red=malloc(n*sizeof(int));
  82. red[0]=0;
  83. cevi[0].stanje='T';
  84. cevi[0].ind=1;
  85. for(i=1; i<n; i++){
  86. cevi[i].stanje='H';
  87. cevi[i].ind=0;
  88. }
  89.  
  90. for(i=0; i<n-1; i++){
  91. scanf("%d%d", &a, &b);
  92. ubaci(cevi+(a-1), b-1);
  93. ubaci(cevi+(b-1), a-1);
  94. }
  95. napraviStablo(cevi);
  96. nadji(cevi);
  97. return 0;
  98. }
  99. //roditelji u pogresnom redosledu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement