Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4. struct nod{
  5. int val;
  6. nod *next;
  7. }*arbore[110];
  8. int subarbore[110],n,k,rad;
  9. void citire()
  10. {
  11. cin>>n>>k;
  12. for (int i=1;i<=n;i++)
  13. {
  14. int x;
  15. cin>>x;
  16. if (x!=0){
  17. nod *nou=new nod;
  18. nou->val=i;
  19. nou->next=arbore[x];
  20. arbore[x]=nou;
  21. }
  22. else
  23. rad=i;//nu ne trebuie in problema asta
  24. }
  25. }
  26. //parcurgere in latime pt a crea coada
  27. void parcurgere(int x)
  28. {
  29. int q[110];
  30. int p=0,u=0;
  31. subarbore[x]++;
  32. q[0]=x;
  33. while (p<=u)
  34. {
  35. int c=q[p];
  36. for (nod *f=arbore[c];f!=NULL;f=f->next)
  37. {
  38. q[++u]=f->val;
  39. subarbore[q[u]]++;
  40. }
  41. p++;
  42. }
  43. }
  44. int main()
  45. {
  46. freopen("subarbore.in","r",stdin);
  47. freopen("subarbore.out","w",stdout);
  48. citire();
  49. parcurgere(k);
  50. for (int i=1;i<=n;i++)
  51. {
  52. if (subarbore[i]!=0)
  53. cout<<i<<" ";
  54. }
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement