跳到正文

个人记录

ZQOI Round 1 题解

tzhengqing约 5 分钟
原文
文章信息与版本

文章信息

发布
2024年7月25日
收录
2025年12月12日
更新
2026年10月12日
篇幅
约 5 分钟

版本

  1. v1当前

题解由@zer20101226,@yangyafan,@tzhengqing提供。

A:

换根dp板子题。

其实就是dfs。

代码实现:

C++
#include<bits/stdc++.h>
using namespace std;
long long n,x,z,y,ans;
struct as{
	long long q,lujing;
};
int s[3001];
vector<as> p[3001];
void dfs(long long m,long long lu){
	if(s[m]==0){
		s[m]=1;
		ans+=lu;
	}
	else return;
	for(long long i=0;i<p[m].size();i++)
		dfs(p[m][i].q,lu+p[m][i].lujing);
		s[m]=0;
	}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n;
	for(long long i=1;i<n;i++){
		cin>>x>>y>>z;
		p[x].push_back(as{y,z});
		p[y].push_back(as{x,z});
	}
	for(long long i=1;i<=n;i++){
		dfs(i,0);
	}cout<<ans%20111113;
	return 0;
}

B:

dp,设dp[i][j]表示到第 ii 层时使用 jj 个传送器的最小花费。

代码:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int w[500005],dp[500005][105],ans=1e18;
signed main(){
	int n,m,q,k,c1,c2;
	cin >>n>>m>>q>>k>>c1>>c2;
	for(int i=1;i<=m;i++) cin >>w[i];
	for(int i=1;i<=m+1;i++) for(int j=0;j<=q;j++) dp[i][j]=min(dp[i-1][j],(i>=k&&j?dp[i-k][j-1]:(int)1e18))+w[i];
	for(int i=0;i<=q;i++) ans=min(ans,dp[m+1][i]);
	if(n<ans){
		cout<<"die\n";
		exit(0);
	}
	cout<<(n-ans)*c1<<" ";
	for(int i=0;i<=q;i++){
		if(n-dp[m+1][i]>=0){
			cout<<i*c2;
			exit(0);
		}
	}
	return 0;
}

C:

LCA诈骗题。只需建立映射,将位置转变为1-n的编号即可。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
struct zzz
	{
	int t,nex;
	}e[1000005];
int head[500010],tot,w[500005];
map<int,int>mp;
void add(int x,int y) 
	{
	e[++tot].t=y;
	e[tot].nex=head[x];
	head[x]=tot;
	}
int depth[500001],fa[500001][22],lg[500001];
void dfs(int now,int fath)
	{
	fa[now][0]=fath;depth[now]=depth[fath]+1;
	for(int i=1;i<=lg[depth[now]];++i)fa[now][i]=fa[fa[now][i-1]][i-1];
	for(int i=head[now];i;i=e[i].nex)
		{
		if(e[i].t!=fath)dfs(e[i].t, now);
		}
	}
int LCA(int x,int y) 
	{
	if(depth[x]<depth[y])swap(x, y);
	while(depth[x]>depth[y])x=fa[x][lg[depth[x]-depth[y]]-1];
	if(x==y)return x;
	for(int k=lg[depth[x]]-1;k>=0;--k)
		{
		if(fa[x][k]!=fa[y][k]){x=fa[x][k];y=fa[y][k];}
		}
	return fa[x][0];
	}
int main()
	{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int n,m;cin>>n>>m;
	for(int i=1;i<=n;i++){cin>>w[i];mp[w[i]]=i;}
	for(int i=1;i<n;++i)
		{
		int x,y;cin>>x>>y;
		x=mp[x];y=mp[y];
		add(x,y);add(y,x);
		}
	for(int i=1;i<=n;++i)lg[i]=lg[i>>1]+1;
	dfs(1,0);
	for(int i=1;i<=m;++i)
		{
		int x,y;cin>>x>>y;
		cout<<w[LCA(mp[x],mp[y])]<<'\n';
		}
	return 0;
	}

D:

因无人打出正解,出题人想要废物利用(会换数据),故不予展示题解。

[数据删除]

原文