Part 思路
思路是简单的,枚举 ,预处理 为子树中距离 为 的点个数,答案即为
Part 暴力
分代码。
#include<bits/stdc++.h>
using namespace std;
int n,p,e[100010],ne[100010],h[100010],idx,cnt[100010][510],de;
long long ans[100010];
void dfs(int i,int d){
cnt[i][0]++;de=max(de,d);
for(int w=h[i];w;w=ne[w]){
dfs(e[w],d+1);
for(int x=1;x<=500;x++)cnt[i][x]+=cnt[e[w]][x-1];
}
}
int main(){
scanf("%d",&n);
for(int w=2;w<=n;w++)scanf("%d",&p),e[++idx]=w,ne[idx]=h[p],h[p]=idx;
dfs(1,1);
for(int w=0;w<=500;w++)for(int x=w;x<=500;x++)for(int y=1;y<=n;y++)
if(w!=x){
long long t=1ll*cnt[y][w]*cnt[y][x];
for(int z=h[y];z;z=ne[z])if(w&&x)t-=1ll*cnt[e[z]][w-1]*cnt[e[z]][x-1];
ans[w^x]+=t;
}else ans[0]+=cnt[y][w]*(cnt[y][w]-1ll)/2;
long long s=0;
for(int w=1;ans[w];w++)s+=ans[w];
printf("%lld\n",1ll*n*(n-1)/2-s);
for(int w=1;ans[w];w++)printf("%lld\n",ans[w]);
return 0;
}Part 初见端倪
,但树的深度不一定是500,不需要枚举到500,枚举到 即可。
分代码。
#include<bits/stdc++.h>
using namespace std;
int n,p,e[100010],ne[100010],h[100010],idx,cnt[100010][510],de;
long long ans[100010];
void dfs(int i,int d){
cnt[i][0]++;de=max(de,d);
for(int w=h[i];w;w=ne[w]){
dfs(e[w],d+1);
for(int x=1;x<=500;x++)cnt[i][x]+=cnt[e[w]][x-1];
}
}
int main(){
scanf("%d",&n);
for(int w=2;w<=n;w++)scanf("%d",&p),e[++idx]=w,ne[idx]=h[p],h[p]=idx;
dfs(1,0);
for(int w=0;w<=de;w++)for(int x=w+1;x<=de;x++){
long long t=0;
for(int y=1;y<=n;y++)t+=1ll*cnt[y][w]*cnt[y][x];
if(w&&x)for(int z=2;z<=n;z++)t-=1ll*cnt[z][w-1]*cnt[z][x-1];
ans[w^x]+=t;
}
long long s=0;
for(int w=1;ans[w];w++)s+=ans[w];
printf("%lld\n",n*(n-1)/2-s);
for(int w=1;ans[w];w++)printf("%lld\n",ans[w]);
return 0;
}Part 继续探索
for(int z=2;z<=n;z++)t-=1ll*cnt[z][w-1]*cnt[z][x-1]; 可以在计算 时顺便维护。
还是 分,但是 AC 的点快了将近一倍
#include<bits/stdc++.h>
using namespace std;
int n,p,e[100010],ne[100010],h[100010],idx,cnt[100010][510],de;
long long ans[100010],f[510][510];
void dfs(int i,int d){
cnt[i][0]++;de=max(de,d);
for(int w=h[i];w;w=ne[w]){
dfs(e[w],d+1);
for(int x=1;x<=500;x++)cnt[i][x]+=cnt[e[w]][x-1];
}
}
int main(){
scanf("%d",&n);
for(int w=2;w<=n;w++)scanf("%d",&p),e[++idx]=w,ne[idx]=h[p],h[p]=idx;
dfs(1,0);
for(int w=0;w<=de;w++)for(int x=w+1;x<=de;x++){
long long t=0;
for(int y=2;y<=n;y++)t+=1ll*cnt[y][w]*cnt[y][x];
f[w][x]=t,t=t+1ll*cnt[1][w]*cnt[1][x];
if(w&&x)t-=f[w-1][x-1];
ans[w^x]+=t;
}
long long s=0;
for(int w=1;ans[w];w++)s+=ans[w];
printf("%lld\n",n*(n-1)/2-s);
for(int w=1;ans[w];w++)printf("%lld\n",ans[w]);
return 0;
}Part 最终の清算
对于一些叶子节点,cnt[y][w] 为 时不需要计算,可省下一些时间。将 数组改为在 dfs 时计算,遇到 cnt[y][w]==0 就停止。
AC 代码:
#include<bits/stdc++.h>
using namespace std;
int n,p,e[100010],ne[100010],h[100010],idx,cnt[100010][510],de;
long long ans[200010],f[510][510];
void dfs(int i,int d){
cnt[i][0]++;de=max(de,d);
for(int w=h[i];w;w=ne[w]){
dfs(e[w],d+1);
for(int x=1;x<=500;x++)cnt[i][x]+=cnt[e[w]][x-1];
}
if(i!=1)for(int x=0;cnt[i][x];x++)for(int y=x+1;cnt[i][y];y++)f[x][y]+=1ll*cnt[i][x]*cnt[i][y];
}
int main(){
scanf("%d",&n);
for(int w=2;w<=n;w++)scanf("%d",&p),e[++idx]=w,ne[idx]=h[p],h[p]=idx;
dfs(1,0);
for(int w=0;w<=de;w++)for(int x=w+1;x<=de;x++){
long long t=f[w][x]+1ll*cnt[1][w]*cnt[1][x];
if(w)t-=f[w-1][x-1];
ans[w^x]+=t;
}
long long s=0;
int t=200000;
while(t)if(ans[t])break;else t--;
for(int w=1;w<=t;w++)s+=ans[w];
printf("%lld\n",1ll*n*(n-1)/2-s);
for(int w=1;w<=t;w++)printf("%lld\n",ans[w]);
return 0;
}甚至卡进了 900ms