Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- struct nod{
- int val;
- nod *next;
- }*arbore[110];
- int subarbore[110],n,k,rad;
- void citire()
- {
- cin>>n>>k;
- for (int i=1;i<=n;i++)
- {
- int x;
- cin>>x;
- if (x!=0){
- nod *nou=new nod;
- nou->val=i;
- nou->next=arbore[x];
- arbore[x]=nou;
- }
- else
- rad=i;//nu ne trebuie in problema asta
- }
- }
- //parcurgere in latime pt a crea coada
- void parcurgere(int x)
- {
- int q[110];
- int p=0,u=0;
- subarbore[x]++;
- q[0]=x;
- while (p<=u)
- {
- int c=q[p];
- for (nod *f=arbore[c];f!=NULL;f=f->next)
- {
- q[++u]=f->val;
- subarbore[q[u]]++;
- }
- p++;
- }
- }
- int main()
- {
- freopen("subarbore.in","r",stdin);
- freopen("subarbore.out","w",stdout);
- citire();
- parcurgere(k);
- for (int i=1;i<=n;i++)
- {
- if (subarbore[i]!=0)
- cout<<i<<" ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement