题目链接  2016 Qingdao Online Problem I

题意  在一棵给定的树上删掉一条边,求剩下两棵树的树的直径中较长那的那个长度的期望,答案乘上$n-1$后输出。

先把原来那棵树的直径求出来。显然删掉的边不是这条直径上的边,那么这时答案就是这条直径的长度。

否则就是直径的某个端点到某一个点(要求连通)的距离的最大值。

在整条链上做两次$DP$之后枚举取较大值即可。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define fi first
#define se second typedef long long LL; const int N = 100010; vector <pair<int, LL > > v[N];
int T, n, L, R, x, cnt;
int a[N], f[N], father[N];
LL b[N], c[N], s[N], cl[N], cr[N], c1, c2, num, ans = 0; void dfs1(int x, int fa, LL now){
if (now > c1){
c1 = now;
L = x;
} for (auto node : v[x]){
int u = node.fi;
LL w = node.se;
if (u == fa) continue;
dfs1(u, x, now + w);
}
} void dfs2(int x, int fa, LL now){
father[x] = fa;
s[x] = now;
if (now > c2){
c2 = now;
R = x;
} for (auto node : v[x]){
int u = node.fi;
LL w = node.se;
if (u == fa) continue;
dfs2(u, x, now + w);
}
} void dfs3(int w, int x, LL now){
f[x] = 1;
c[w] = max(c[w], now);
for (auto node : v[x]){
int u = node.fi;
if (f[u]) continue;
dfs3(w, u, now + node.se);
}
} int main(){ for (scanf("%d", &T); T--; ){
scanf("%d", &n);
rep(i, 0, n + 1) v[i].clear();
rep(i, 2, n){
int x, y;
LL z;
scanf("%d%d%lld", &x, &y, &z);
v[x].push_back({y, z});
v[y].push_back({x, z});
} L = -1; c1 = -1;
dfs1(1, 0, 0);
R = -1, c2 = -1;
memset(father, 0, sizeof father);
dfs2(L, 0, 0); x = R;
cnt = 0;
while (true){
a[++cnt] = R;
R = father[R];
if (R == 0) break;
} rep(i, 1, cnt) b[i] = s[a[i]];
reverse(a + 1, a + cnt + 1);
reverse(b + 1, b + cnt + 1); num = b[cnt];
ans = num * (n - cnt); memset(f, 0, sizeof f);
rep(i, 1, cnt) f[a[i]] = 1; rep(i, 1, cnt){
c[i] = 0;
dfs3(i, a[i], 0);
} cl[1] = b[1]; rep(i, 2, cnt) cl[i] = max(cl[i - 1], b[i] + c[i]);
cr[cnt] = 0; dec(i, cnt - 1, 1) cr[i] = max(cr[i + 1], b[cnt] - b[i] + c[i]);
rep(i, 1, cnt - 1) ans = ans + max(cl[i], cr[i + 1]);
printf("%lld\n", ans);
} return 0;
}

HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)的更多相关文章

  1. HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)

    题目链接  2016 青岛网络赛  Problem C 题意  给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...

  2. HDU 5886 Tower Defence

    树的直径. 比赛的时候想着先树$dp$处理子树上的最长链和次长链,然后再从上到下进行一次$dfs$统计答案,和$CCPC$网络赛那个树$dp$一样,肯定是可以写的,但会很烦.......后来写崩了. ...

  3. HDU 4768 Flyer (2013长春网络赛1010题,二分)

    Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  4. HDU 4758 Walk Through Squares (2013南京网络赛1011题,AC自动机+DP)

    Walk Through Squares Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Oth ...

  5. HDU 4747 Mex (2013杭州网络赛1010题,线段树)

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  6. HDU - 5878 2016青岛网络赛 I Count Two Three(打表+二分)

    I Count Two Three 31.1% 1000ms 32768K   I will show you the most popular board game in the Shanghai ...

  7. HDU - 5887 2016青岛网络赛 Herbs Gathering(形似01背包的搜索)

    Herbs Gathering 10.76% 1000ms 32768K   Collecting one's own plants for use as herbal medicines is pe ...

  8. HDU 6215 2017Brute Force Sorting 青岛网络赛 队列加链表模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]&l ...

  9. HDU5887 Herbs Gathering(2016青岛网络赛 搜索 剪枝)

    背包问题,由于数据大不容易dp,改为剪枝,先按性价比排序,若剩下的背包空间都以最高性价比选时不会比已找到的最优解更好时则剪枝,即 if(val + (LD)pk[d].val / (LD)pk[d]. ...

随机推荐

  1. 【Divided Two】cpp

    题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...

  2. Python 爬虫-豆瓣读书

    import requests from bs4 import BeautifulSoup def parse_html(num): headers = { 'User-Agent': 'Mozill ...

  3. python中字符串是特殊的列表

    for x in range(20): print 'fizz'[x%3*4::]+'buzz'[x%5*4::]or x 这个是由 Jeff Atwood推广的一个编程练习叫FizzBuzz,问题如 ...

  4. python学习总结---面向对象2

    面向对象三大特点 - 封装:既是对数据结构的封装,有是处理数据的方法的封装. - 继承:强调的父子类的关系. - 多态:不同对象调用相同的方法,有不同的响应. 类的继承 - 相关概念 - 继承:父类的 ...

  5. Where can I find the IPA logs

    Retrieving the IPA logs will differ depending on which base image was used. Operating system that do ...

  6. 网络--OSI七层模型详解

    OSI 七层模型通过七个层次化的结构模型使不同的系统不同的网络之间实现可靠的通讯,因此其最主要的功能就是帮助不同类型的主机实现数据传输 . 完成中继功能的节点通常称为中继系统.在OSI七层模型中,处于 ...

  7. 团队冲刺Alpha(七)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  8. spring security注解(1)

    Chapter 15. 基于表达式的权限控制 Spring Security 3.0介绍了使用Spring EL表达式的能力,作为一种验证机制 添加简单的配置属性的使用和访问决策投票,就像以前一样. ...

  9. 【bzoj4822/bzoj1935】[Cqoi2017]老C的任务/[Shoi2007]Tree 园丁的烦恼 树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825530.html bzoj4822 题目描述 老 C 是个程序员.     最近老 C 从老板那里接到了一个任务 ...

  10. MPLAB® XC C编译器的Workstation License的获取及安装方法

    MPLAB®XC C编译器的Workstation License获取及安装方法如下:首先需要购买获得一个XC C编译器的激活码,然后到以下网页(http://www.microchip.com/rl ...