刷题记录:牛客NC52867Highway [求树的直径]

传送门:牛客

题目描述:

In ICPCCamp there were n towns conveniently numbered with 1, 2, \dots, n1,2,…,n
connected with (n - 1) roads.
The i-th road connecting towns ai and bi has length ci.
It is guaranteed that any two cities reach each other using only roads.
Bobo would like to build (n - 1) highways so that any two towns reach each using *only highways*.
Building a highway between towns x and y costs him \delta(x, y)δ(x,y) cents,
where δ(x,y) is the length of the shortest path between towns x and y using roads.
As Bobo is rich, he would like to find the most expensive way to build the (n - 1) highways.
输入:
5
1 2 2
1 3 1
2 4 2
3 5 1
5
1 2 2
1 4 1
3 4 1
4 5 2
输出:
19
15

一道关于树的直径(树上最长链)和最短路的问题

这道题的重点是要发现点数等于边数+1,这样我们的图就变成了一棵树,我们就可以使用树的一些性质了

主要思路:

  1. 把玩一下题目,我们会发现既然是一颗树,我们还需要求出最大的权值和.那么对于每一个点(也就是联通分量来说),两点之间存在一条路径,我们需要做的是找出最大的n-1条路径
  2. 到此时其实我们有一个朴素的想法,直接求出树上两点之间的距离(跑LCA),然后就可以使用最大生成树来解决这道题了.但很不幸,这么做会MLE,我们的点数是在太多了,存不下来.那么此时该怎么进行优化呢.

此时引入树的直径.树的直径是树上两点之间最长的距离.那么对于本题来说,树的直径对应的距离显然符合最大的n-1条路径.所以我们可以基于直径的两点进行操作.

首先树的直径有这样的一个性质,也就是树上的点到另一个点的最远距离,就是它到直径中的一个端点的距离.这是我们解题的关键

首先在解题之前浅浅的证明一下吧(不然肯定感觉很云里雾里的):

我们将直径的两个端点记为A,B,中间点记为K,不妨设AK>BK,现在我们假设A并不是K到所有点中最长的点,那么存在CK>AK,现在关于我们的K有两种情况:


第一种是我们的K在树的直径上,那么显然此时我们的直径应该是CB,因为CK+KB>AK+KB,和我们的AB是树的直径矛盾
第二种是我们的K不在树的直径上,那么我们设有一点G在树的直径上,K通过一系列点在G处与直径联通,不管是多少,方便起见,我们累加一下记为KG,那么此时我们有CK>AK,又有AK=AG+GK(因为是树,所以不存在环,所以K与直径上相连的点只有G),那么就有CK>AG+GK,那么显然我们的CG>AG,显然此时CB应该是直径,和我们的AB是直径矛盾

至此,证毕


所以接下来直接使用这个结论就行.
我们先随便取一个点设为根,用DFS跑一下到每一个点的距离(因为是树,所以没必要跑 d i j k s t r a dijkstra dijkstra),找出最长的那个端点,按照我们上面的证明,显然这个点是我们的直径的端点之一.
然后再以上面的这个端点跑DFS,找出距离他最长的点,显然这个点是直径的另一个端点
接下来我们只需要求出每一个点到这两个端点的距离去max即可,对于这个相当于求这两个点到其余点的距离

注意本题需要使用longlong

下面是具体的代码部分:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <stack>
#include <deque>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
	ll x=0,w=1;char ch=getchar();
	for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
	for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x*w;
}
#define int long long
#define maxn 1000000
#define ll_maxn 0x3f3f3f3f3f3f3f3f
const double eps=1e-8;
int n;
struct Node{
	int v,w;
};
vector<Node>edge[100010];
int dis[100010];
void dfs1(int u,int pre) {//求第一个最远端点
	for(int i=0;i<edge[u].size();i++) {
		int v=edge[u][i].v;
		if(v==pre) continue;
		dis[v]=dis[u]+edge[u][i].w;
		dfs1(v,u);
	}
}
int dis2[100010];
void dfs2(int u,int pre) {
	for(int i=0;i<edge[u].size();i++) {
		int v=edge[u][i].v;
		if(v==pre) continue;
		dis2[v]=dis2[u]+edge[u][i].w;
		dfs2(v,u);
	}
}
signed main() {
	while(scanf("%d",&n)!=EOF) {
		for (int i=1;i<=n;i++) edge[i].clear();
		memset(dis,0,sizeof(dis));
		memset(dis2,0,sizeof(dis2));
		for(int i=1;i<=n-1;i++) {
			int u=read(),v=read(),w=read();
			edge[u].push_back({v,w});
			edge[v].push_back({u,w});
		}
		dfs1(1,0);int ans=-inf,node1=1;
		for(int i=2;i<=n;i++) {
			if(dis[i]>ans) {
				ans=dis[i];node1=i;
			}
		}
		dfs2(node1,0);
		ans=-inf;int node2;
		for(int i=1;i<=n;i++) {
			if(dis2[i]>ans) {
				ans=dis2[i];node2=i;
			}
		}
		memset(dis,0,sizeof(dis));
		dfs1(node2,0);
		ans=0;
		for(int i=1;i<=n;i++) {
			if(i==node1) continue;
			ans+=max(dis[i],dis2[i]);
		}
		printf("%lld\n",ans);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值