HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)
题目链接 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)的更多相关文章
- HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)
题目链接 2016 青岛网络赛 Problem C 题意 给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...
- HDU 5886 Tower Defence
树的直径. 比赛的时候想着先树$dp$处理子树上的最长链和次长链,然后再从上到下进行一次$dfs$统计答案,和$CCPC$网络赛那个树$dp$一样,肯定是可以写的,但会很烦.......后来写崩了. ...
- HDU 4768 Flyer (2013长春网络赛1010题,二分)
Flyer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 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 ...
- HDU 4747 Mex (2013杭州网络赛1010题,线段树)
Mex Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- 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 ...
- HDU - 5887 2016青岛网络赛 Herbs Gathering(形似01背包的搜索)
Herbs Gathering 10.76% 1000ms 32768K Collecting one's own plants for use as herbal medicines is pe ...
- HDU 6215 2017Brute Force Sorting 青岛网络赛 队列加链表模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]&l ...
- HDU5887 Herbs Gathering(2016青岛网络赛 搜索 剪枝)
背包问题,由于数据大不容易dp,改为剪枝,先按性价比排序,若剩下的背包空间都以最高性价比选时不会比已找到的最优解更好时则剪枝,即 if(val + (LD)pk[d].val / (LD)pk[d]. ...
随机推荐
- 新生 & 语不惊人死不休 —— 《无限恐怖》读后有感
开篇声明,我博客中“小心情”这一系列,全都是日记啊随笔啊什么乱七八糟的.如果一不小心点进来了,不妨直接关掉.我自己曾经写过一段时间的日记,常常翻看,毫无疑问我的文笔是很差的,而且心情也是瞬息万变的.因 ...
- Adaptive Boosting
Boosting boosting和bagging很类似,所使用的多个分类器类型都是一致的.另外,他们的主要区别点如下: boosting中不同的分类器是通过串行得到的,每个分类器都是根据已经训练出来 ...
- Opencv3.4.5安装包
这个资源是Opencv3.4.5安装包,包括Windows软件包,Android软件包,IOS软件包,还有opencv的源代码:需要的下载吧. 点击下载
- [转]Docker学习笔记之一,搭建一个JAVA Tomcat运行环境
本文转自:http://www.blogjava.net/yongboy/archive/2013/12/12/407498.html 前言 Docker旨在提供一种应用程序的自动化部署解决方案,在 ...
- java与C#对比文章阅读
文章:JAVA与C#的区别 讲了C#与java一些基本异同. 易语言官网有个表,比较了易语言.Java.C#的区别,比较全面可以借鉴.
- 【python】用python爬取中科院院士简介信息
018/07/09 23:43 项目名称:爬取中科院871个院士的简介信息 1.爬取目的:中科院871个院士的简介信息 2.爬取最终结果: 3.具体代码如下: import re # 不用安装(注意! ...
- 【NOI 2015网络同步赛】
今年没法去.. 分数160+181+100(假设我有去考笔试)=441 分数线:金548 银459 铜331 并没有到银牌线.... 以后题目啊数据啊出来的话继续更新 2015.7.19
- [poj] 1235 Farm Tour || 最小费用最大流
原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...
- 多校4 lazy running (最短路)
lazy running(最短路) 题意: 一个环上有四个点,从点2出发回到起点,走过的距离不小于K的最短距离是多少 \(K <= 10^{18} 1 <= d <= 30000\) ...
- 【转】去掉HTML5中number类型input字段的小箭头
第一种方案: 在chrome下: input::-webkit-outer-spin-button,input::-webkit-inner-spin-button{ -webkit-appea ...