消防局的设立

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 const int maxn=1007;
 7 const int INF=0x7f7f7f7f;
 8 int n,ans;
 9 int fa[maxn],a[maxn],dis[maxn],dep[maxn];
10 bool cmp(int x,int y){
11   return dep[x]>dep[y];
12 }
13 int main(){
14   memset(dis,INF,sizeof(dis));
15   cin>>n;a[1]=1;fa[1]=0;
16   for(int i=2;i<=n;i++){
17     cin>>fa[i];a[i]=i;
18     dep[i]=dep[fa[i]]+1;
19   }
20   sort(a+1,a+n+1,cmp);
21   for(int i=1;i<=n;i++){
22     int u=a[i];int v=fa[u];int w=fa[v];
23     dis[u]=min(dis[u],min(dis[v]+1,dis[w]+2));
24     if(dis[u]>2){
25       dis[w]=0;ans++;
26       dis[fa[w]]=min(dis[fa[w]],1);
27       dis[fa[fa[w]]]=min(dis[fa[fa[w]]],2);
28     }
29   }
30   cout<<ans<<endl;
31   return 0;
32 }

 

posted @ 2018-10-12 21:07  lcan  阅读(234)  评论(0编辑  收藏  举报