观察发现对于树的每个子树都是一个相同的子问题,而每个问题其实就是哈夫曼编码,所以从底层开始每个点对他的所有儿子做一次哈夫曼即可
。。父亲数组不是有序的,所以要记录一下编号然后排个序,就可以从后往前直接枚举了……
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define MAXN 100010
#define MAXM 1010
#define ll long long
#define INF 1000000000
#define MOD 1000000007
#define eps 1e-8
struct data{
int s;
int v;
data(){
}
data(int _s){
s=_s;
}
friend bool operator <(data x,data y){
return x.s>y.s;
}
};
struct tree{
int fa;
int num;
friend bool operator <(tree x,tree y){
return x.fa<y.fa;
}
};
tree tr[MAXN];
priority_queue<data>q;
int n;
int s[MAXN];
ll ans;
int main(){
int i;
scanf("%d",&n);
s[1]=1;
for(i=2;i<=n;i++){
scanf("%d",&tr[i].fa);
tr[i].num=i;
s[i]=1;
}
sort(tr+1,tr+n+1);
for(i=n;i!=1;i--){
int tf=tr[i].fa;
while(tr[i].fa==tf){
q.push(data(s[tr[i].num]));
i--;
}
i++;
while(q.size()!=1){
data t1=q.top();
q.pop();
data t2=q.top();
q.pop();
data t;
t.s=t1.s+t2.s;
ans+=t.s;
q.push(t);
}
s[tf]=q.top().s+1;
q.pop();
}
printf("%lld\n",ans);
return 0;
}
/*
6
1 2 1 3 2
6
1 1 2 2 3
4
1 2 3
*/