跳到正文

个人记录

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

Liyunze123约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年6月3日
篇幅
约 7 分钟

版本

  1. v1当前

Part 00 思路

思路是简单的,枚举 L1,L2L_1,L_2,预处理 cnti,jcnt_{i,j} 为子树中距离 ii 为 jj 的点个数,答案即为 ∑i=1ncnti,L1×cnti,L2−∑i=2ncnti,L1−1×cnti,L2−1\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 11 暴力

7070 分代码。

C++
#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 22 初见端倪

H≤500H \le 500,但树的深度不一定是500,不需要枚举到500,枚举到 depdep 即可。

8080 分代码。

C++
#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 33 继续探索

for(int z=2;z<=n;z++)t-=1ll*cnt[z][w-1]*cnt[z][x-1]; 可以在计算 (L1−1,L2−1)(L_1-1,L_2-1) 时顺便维护。

还是 8080 分,但是 AC 的点快了将近一倍

C++
#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 44 最终の清算

对于一些叶子节点,cnt[y][w] 为 00 时不需要计算,可省下一些时间。将 ff 数组改为在 dfs 时计算,遇到 cnt[y][w]==0 就停止。

AC 代码:

C++
#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

原文