跳到正文

个人记录

50分+100分

AC_Seed约 9 分钟
原文
文章信息与版本

文章信息

收录
2026年4月11日
更新
2026年4月11日
篇幅
约 9 分钟

版本

  1. v4当前
  2. v3
  3. v2
  4. v1
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
struct node{
	int x;
	int step;
};
vector<node>g[maxn];
int n;
int k=0,maxi=0;
int diss[maxn];
int dp[maxn];
void dfs(int x,int fa,int dis){
	diss[x]=dis;
	for(int i=0;i<g[x].size();i++){
		int v=g[x][i].x;
		int w=g[x][i].step;
		if(v==fa) continue;
		dfs(v,x,dis+w);
	}
}
void work(int x){
	dfs(x,0,0);
	int maxi=0;
	for(int i=1;i<=n;i++) maxi=max(maxi,diss[i]);
	dp[x]=maxi;
	return ;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<n;i++){
		int x,y,z;
		cin>>x>>y>>z;
		g[x].push_back({y,z});
		g[y].push_back({x,z});
	}
	for(int i=1;i<=n;i++){
		work(i);
	}
	int mini=1e9;
	for(int i=1;i<=n;i++){
		mini=min(mini,dp[i]);
	}
	for(int i=1;i<=n;i++){
		if(dp[i]==mini){
			cout<<i<<"\n";
		}
	}
	return 0;
}
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
struct node{
	int x;
	int step;
};
vector<node>g[maxn];
int n;
int dp1[maxn],dp2[maxn];
int son[maxn];
int dp3[maxn];
void dfs(int x,int fa){
	for(int i=0;i<g[x].size();i++){
		int v=g[x][i].x;
		int w=g[x][i].step;
		if(v==fa) continue;
		dfs(v,x);
		int tmp=dp1[v]+w;
		if(tmp>=dp1[x]){
			son[x]=v;
			dp2[x]=dp1[x];
			dp1[x]=tmp;
		}
		else if(tmp>dp2[x]){
			dp2[x]=tmp;
		}
	}
	return ;
}
void dfs2(int x,int fa){
    for(int i=0;i<g[x].size();i++){
    	int v=g[x][i].x;
    	int w=g[x][i].step;
    	if(v==fa) continue;
    	dp3[v]=dp3[x]+w;
    	if(son[x]==v){
    		dp3[v]=max(dp3[v],dp2[x]+w);
		}
		else{
			dp3[v]=max(dp3[v],dp1[x]+w);
		}
		dfs2(v,x);
	}
	return ;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<n;i++){
		int x,y,z;
		cin>>x>>y>>z;
		g[x].push_back({y,z});
		g[y].push_back({x,z});
	}
	dfs(1,0);
	dfs2(1,0);
	int mini=1e9;
	for(int i=1;i<=n;i++){
		mini=min(mini,max(dp1[i],dp3[i]));
	}
	for(int i=1;i<=n;i++){
		if(max(dp1[i],dp3[i])==mini){
			cout<<i<<"\n";
		}
	}
	return 0;
}
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=2e5+5;
struct node{
	int x;
	int step;
};
int T;
vector<node>g[maxn];
int dep[maxn];
int n;
int dp[maxn];
int dp2[maxn];
int maxx=0;
void dfs(int u,int fa){
	for(int i=0;i<g[u].size();i++){
		int v=g[u][i].x;
		int w=g[u][i].step;
		if(v==fa) continue;
		dfs(v,u);
		if(dep[v]==1){
			dp[u]+=w;
		}
		else{
			dp[u]+=min(dp[v],w);
		}
	}
}
void dfs2(int u,int fa){
	for(int i=0;i<g[u].size();i++){
		int v=g[u][i].x;
		int w=g[u][i].step;
		if(v==fa) continue;
		if(dep[u]==1){
			dp2[v]=w+dp[v];
		}
		else if(dep[v]==1){
			dp2[v]=min(dp2[u]-w,w);
		}
		else{
			dp2[v]=dp[v]+min(dp2[u]-min(w,dp[v]),w);
		}
		maxx=max(maxx,dp2[v]);
		dfs2(v,u);
	}
	return ;
}
void work(){
	cin>>n;
	for(int i=1;i<=n;i++){
		g[i].clear();
		dp[i]=dp2[i]=dep[i]=0;
	}
	for(int i=1;i<n;i++){
		int u,v,w;
		cin>>u>>v>>w;
		g[u].push_back({v,w});
		g[v].push_back({u,w});
		dep[u]++;
		dep[v]++;
	}
	dfs(1,0);
	maxx=dp2[1]=dp[1];
	dfs2(1,0);
	cout<<maxx<<"\n";
	return ;
}
signed main(){
	cin>>T;
	while(T--) work();
	return 0;
}
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
struct node{
	int x;
	int step;
};
vector<node>g[maxn];
int n;
int dp1[maxn],dp2[maxn];
int son[maxn];
int dp3[maxn];
void dfs(int x,int fa){
	for(int i=0;i<g[x].size();i++){
		int v=g[x][i].x;
		int w=g[x][i].step;
		if(v==fa) continue;
		dfs(v,x);
		int tmp=dp1[v]+w;
		if(tmp>=dp1[x]){
			son[x]=v;
			dp2[x]=dp1[x];
			dp1[x]=tmp;
		}
		else if(tmp>dp2[x]){
			dp2[x]=tmp;
		}
	}
	return ;
}
void dfs2(int x,int fa){
    for(int i=0;i<g[x].size();i++){
    	int v=g[x][i].x;
    	int w=g[x][i].step;
    	if(v==fa) continue;
    	dp3[v]=dp3[x]+w;
    	if(son[x]==v){
    		dp3[v]=max(dp3[v],dp2[x]+w);
		}
		else{
			dp3[v]=max(dp3[v],dp1[x]+w);
		}
		dfs2(v,x);
	}
	return ;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	while(cin>>n){
	for(int i=1;i<n;i++){
		int x,z;
		cin>>x>>z;
		g[i].push_back({x-1,z});
		g[x-1].push_back({i,z});
	}
	dfs(0,-1);
	dfs2(0,-1);
	int mini=1e9;
	for(int i=0;i<n;i++){
		cout<<max(dp1[i],dp3[i])<<"\n";
	}
    for(int i=0;i<=n;i++){
        dp1[i]=dp2[i]=dp3[i]=0;
        g[i].clear();
    }
	}
	return 0;
}
原文