传送门

题意

有n个小镇,Bobo想要建造n-1条边,并且如果在u到v建边,那么花费是u到v的最短路长度(原图),问你最大的花费。

分析

比赛的时候没做出来,QAQ

我们首先要找到树的直径起点和终点,方法是

1.任意选一个点,dfs找到最长路的终点

2.从终点反向dfs,找到起点

然后枚举每个点,用倍增lca求出该点到起点与终点距离,取距离大的,注意直径本身会被访问两次,故一开始ans要减去最长路,时间复杂度O(nlogn)

代码

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std; #define ll long long
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))
#pragma comment(linker, "/STACK:102400000,102400000")
inline void read(int &x){x=0; char ch=getchar();while(ch<'0') ch=getchar();while(ch>='0'){x=x*10+ch-48; ch=getchar();}} int t,n,m;
int fa[100100],depth[100100],up[100100][20],vis[100100];
ll dis[100100],dis2[100100];
struct node
{
int to;
ll w;
node(int tt,ll ww):to(tt),w(ww){}
};
vector<node>edge[100100]; void dfs(int u,int pre,int dep)//第一次dfs,标出每个点的父亲,计算从该点到1的距离dus[i]
{
depth[u]=dep;//记录深度
fa[u]=pre;//记录父亲
int num=edge[u].size();
R(i,0,num)
{
int v=edge[u][i].to;
if(v!=pre)
{
dis[v]=dis[u]+edge[u][i].w;//更新距离
dfs(v,u,dep+1);
}
}
}
void dfs2(int x)//第二次遍历,从第一次dfs的最大距离点遍历
{
int num=edge[x].size();
vis[x]=1;//用vis记录该点是否被访问
R(i,0,num)
{
int v=edge[x][i].to;
if(!vis[v])
{
dis2[v]=dis2[x]+edge[x][i].w;//记录距离
dfs2(v);
}
}
}
void create()//构建倍增数组
{
mem(up,0);
F(i,1,n) up[i][0]=fa[i];
for(int j=1;j<20;++j)for(int i=1;i<=n;++i)
up[i][j]=up[up[i][j-1]][j-1];
}
int lca(int u,int v)//找到u和v的的最近公共祖先
{
if(depth[u]<depth[v]) swap(u,v);
int ret=depth[u]-depth[v];
R(i,0,20)if(ret&(1<<i)) u=up[u][i];//将u调整到与v同深度
if(u==v) return u;//同一侧,则返回
for(int i=19;i>=0;--i) if(up[u][i]!=up[v][i])//不同则让u和v向上跳,直到跳不了
{
u=up[u][i];
v=up[v][i];
}
return fa[u];
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int u,v;ll w;
F(i,0,n) edge[i].clear();
mem(fa,0);mem(depth,0);
mem(dis,0);mem(dis2,0);//注意dis和dis2的清空
for(int i=1;i<n;++i)
{
scanf("%d%d%I64d",&u,&v,&w);
edge[u].push_back(node(v,w));
edge[v].push_back(node(u,w));
}
dfs(1,0,0);
ll maxx=0;int End,Begin;
F(i,1,n) if(maxx<dis[i]) { maxx=dis[i];End=i; }//记录从1开始的最长路终点
create();
mem(vis,0);
dfs2(End);
maxx=0;
F(i,1,n) if(maxx<dis2[i]) { maxx=dis2[i];Begin=i; }//找到反向dfs最长路的点,那么Begin和End构成树的直径
ll ans=-maxx;//要遍历树的直径两次,故先减去一次
F(i,1,n)
{
ll ret1,ret2;
ret1=dis[i]+dis[Begin]-2*dis[lca(i,Begin)];
ret2=dis[i]+dis[End]-2*dis[lca(i,End)];
ans+=max(ret1,ret2);//去当前点到直径两端的最短路长度的最大值
}
printf("%I64d\n",ans);
}
return 0;
}

XTU1267:Highway(LCA+树的直径)的更多相关文章

  1. TZOJ 3481 Highway Construction(树的直径+最短路)

    描述 As head of the Accessible Commuting Movement (ACM), you've been lobbying the mayor to build a new ...

  2. XTOJ 1267:Highway(树的直径)***

    http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1267 题意:给出一棵树,每条树边有权值,现在要修建n-1条边,边的权值为边 ...

  3. 【CSP模拟赛】避难向导(倍增lca&树的直径)

    耐力OIer,一天7篇博客 题目描述 “特大新闻,特大新闻!全国爆发了一种极其可怕的病毒,已经开始在各个城市 中传播开来!全国陷入了巨大的危机!大量居民陷入恐慌,想要逃到其它城市以 避难!经调查显示, ...

  4. BZOJ 1776: [Usaco2010 Hol]cowpol 奶牛政坛 LCA + 树的直径

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...

  5. XTU 1267 - Highway - [树的直径][2017湘潭邀请赛H题(江苏省赛)]

    这道题可能有毒……总之一会儿能过一会儿不能过的,搞的我很心烦…… 依然是上次2017江苏省赛的题目,之前期末考试结束了之后有想补一下这道题,当时比较懵逼不知道怎么做……看了题解也不是很懂……就只好放弃 ...

  6. 51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径

    51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径 题面 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即 ...

  7. 【bzoj3362/3363/3364/3365】[Usaco2004 Feb]树上问题杂烩 并查集/树的直径/LCA/树的点分治

    题目描述 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂直或水平的道路连结着农场,道路的长度不超过1000.这些农场的分布就像下面的地图一样, 图中农场用F ...

  8. [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树)

    [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树) 题面 给出一棵N个点的树,Q次询问一点编号在区间[l1,r1]内,另一点编号在区间[l2,r2]内的所有点对距离最大值.\ ...

  9. 树的直径,LCA复习笔记

    前言 复习笔记第6篇. 求直径的两种方法 树形DP: dfs(y); ans=max( ans,d[x]+d[y]+w[i] ); d[x]=max( d[x],d[y]+w[i] ); int di ...

随机推荐

  1. TCP/IP Protocol Architecture

    原文: https://technet.microsoft.com/en-sg/library/cc958821.aspx 1. 主机到网络层 2.网络互连层(互连这个翻译好) ----------- ...

  2. [React] Persist Form Data in React and Formik with formik-persist

    It can be incredibly frustrating to spend a few minutes filling out a form only to accidentally lose ...

  3. 经典游戏“大富翁4”存档文件修改器Rich4Editor下载

    下载地址: http://files.cnblogs.com/files/xiandedanteng/Rich4Editor20170614.zip http://files.cnblogs.com/ ...

  4. 《Deep Learning》全书已完稿_附全书电子版

    Deep Learning第一篇书籍最终问世了.站点链接: http://www.deeplearningbook.org/ Bengio大神的<Deep Learning>全书电子版在百 ...

  5. Qt linux文件同步写入

    因为linux 系统机制问题,文件的创建和写入并不会直接写入硬盘.而是先写入缓存,当系统要关闭或须要时才写入硬盘.为防止突然掉电,应将缓存中的文件及时同步到硬盘上去. linux 下的sync 命令具 ...

  6. grunt简单教程

    Grunt简单教程 1.grunt简单介绍 Grunt是一个基于任务的命令行工具.依赖于node.js环境. 它能帮你合并js文件,压缩js文件,验证js.编译less,合并css.还能够配置自己主动 ...

  7. LIS(最长上升子序列)的三种经典求法

    求最长上升子序列的三种经典方案: 给定一个长度为 \(N\) 的数列,求它数值单调递增的子序列长度最大为多少.即已知有数列 \(A\) , \(A=\{A_1,A_2....A_n\}\) ,求 \( ...

  8. install build tools 25.0.2 and sync the project

    install build tools 25.0.2 and sync the project in android studio bundle.gradle,将buildToolsVersion修改 ...

  9. sdut oj 1510 Contest02-4 Spiral

    Contest02-4 Spiral Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Given an odd number n, ...

  10. YTU 2578: 分数减法——结构体

    2578: 分数减法--结构体 时间限制: 1 Sec  内存限制: 128 MB 提交: 522  解决: 399 题目描述 分数可以看成是由字符'/'分割两个整数构成,可以用结构体类型表示.请用结 ...