(疑似卡常?)xmoj10901优化实录

· · 个人记录

Part 0 思路

思路是简单的,枚举 L_1,L_2,预处理 cnt_{i,j} 为子树中距离 i 为 j 的点个数,答案即为 \sum^n_{i=1} cnt_{i,L1}\times cnt_{i,L2} - \sum^n_{i=2} cnt_{i,L1-1}\times cnt_{i,L2-1}

Part 1 暴力

``` #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 $2$ 初见端倪 $H \le 500$,但树的深度不一定是500,不需要枚举到500,枚举到 $dep$ 即可。 $80$ 分代码。 ``` #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 $3$ 继续探索 `for(int z=2;z<=n;z++)t-=1ll*cnt[z][w-1]*cnt[z][x-1];` 可以在计算 $(L_1-1,L_2-1)$ 时顺便维护。 还是 $80$ 分,但是 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 $4$ 最终の清算 对于一些叶子节点,`cnt[y][w]` 为 $0$ 时不需要计算,可省下一些时间。将 $f$ 数组改为在 `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~~