题目链接  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. 《Cracking the Coding Interview》——第1章:数组和字符串——题目8

    2014-03-18 02:12 题目:判断一个字符串是否由另一个字符串循环移位而成. 解法:首先长度必须相等.然后将第一个串连拼两次,判断第二个串是否在这个连接串中. 代码: // 1.8 Assu ...

  2. Pascal编写的蠕虫病毒,凌盟提供,Chaobs转载

    { Happy Birthday (c) 1998 WoRmI don't take responsibility for any damage caused by this virus.It was ...

  3. Nuget 异常引用记录

    事件描述 Nuget未能将packages.config中的dll成功引入项目中 解决办法 从Nuget中删除对NewtonSoft.Json的引用并重新对NewtonSoft.Json 4.5.0. ...

  4. Kotlin将Realm提升到更高层次

    作者:Víctor Manuel Pineda 时间:Feb 14, 2017 原文链接:https://antonioleiva.com/kotlin-realm-extensions/ 当有人问我 ...

  5. time模块与random模块,六位含字母随机验证码

    # time模块# import time# time.time()#计算这一时刻时间戳 *******# time.sleep(1)#让cpu休眠一定时间 *******# time.clock() ...

  6. day04_07-三个函数的区别

    <?php $link = @mysql_connect('localhost','root',''); mysql_query('use test',$link); mysql_query(' ...

  7. 把SVN版本控制讲给 非IT同事 听

    场景: 什么是版本: 什么是版本控制: 为什么要用版本控制: 推荐使用SVN: 如何快速理解SVN: SVN简单使用:

  8. [OpenCV] Ptr类模板

    1.C++泛型句柄类 我们知道在包含指针成员的类中,需要特别注意类的复制控制,因为复制指针时只复制指针中的地址,而不会复制指针指向的对象.这将导致当两个指针同时指向同一对象时,很可能一个指针删除了一对 ...

  9. PHP命名空间与use

    当在一个大型项目很多程序员书写模板时,最怕出现的问题就是命名,如果一个PHP脚本出现了同名的类或者方法,就会报错(fatal error),使用命名空间可以 解决这个问题 知识点: 命名空间names ...

  10. bzoj2441 [中山市选2011]小W的问题(debug中)

    2441: [中山市选2011]小W的问题 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 487  Solved: 186[Submit][Statu ...