http://uoj.ac/problem/105

好神的dp啊。

确定一个点为根之后,蓝线只能是竖着的,不能横跨兄弟。

枚举每个点为根进行树形dp是\(O(n^2)\)的,\(f(x,0/1)\)表示以\(x\)为根的子树中\(x\)是否作为蓝线终点的最大值。

更科学的做法:\(O(1)\)把根从一个father转移到它的son。

需要维护\(f(father,1)\)的最大和次大(防止son作为最大转移到father),利用father的信息更新\(f(son,0)\)和\(f(son,1)\)的最大和次大(这里的换根不是真正把根换到son,只是说换根后son作为根的信息是正确的,不需要修改father的信息)。

时间复杂度\(O(n)\)。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N = 200003; struct node {int nxt, to, w;} E[N << 1];
int cnt = 0, point[N], f[N][2], fs[N][2]; void ins(int u, int v, int w) {E[++cnt] = (node) {point[u], v, w}; point[u] = cnt;} int n, son[N], tot, fadis[N]; void dfs(int x, int fa) {
for (int i = point[x]; i; i = E[i].nxt)
if (E[i].to != fa) fadis[E[i].to] = E[i].w, dfs(E[i].to, x); tot = 0;
for (int i = point[x]; i; i = E[i].nxt)
if (E[i].to != fa) son[++tot] = E[i].to; for (int i = 1; i <= tot; ++i)
f[x][0] += max(f[son[i]][0], f[son[i]][1]); int mx = -0x7fffffff, mxs = -0x7fffffff, num;
for (int i = 1; i <= tot; ++i) {
num = f[son[i]][0] + fadis[son[i]] - max(f[son[i]][0], f[son[i]][1]);
if (num >= mx) mxs = mx, mx = num;
else if (num > mxs) mxs = num;
} f[x][1] = mx + fadis[x] + f[x][0];
fs[x][1] = mxs + fadis[x] + f[x][0];
} void move(int x, int y, int d) {
int t, fx0 = f[x][0], fx1 = f[x][1], fxs1 = fs[x][1];
if (fx1 == fx0 - max(f[y][1], f[y][0]) + f[y][0] + fadis[y]) fx1 = fxs1;
fx0 -= max(f[y][1], f[y][0]);
fx1 -= max(f[y][1], f[y][0]);
fx1 += d;
f[y][0] += max(fx0, fx1);
f[y][1] += max(fx0, fx1);
fs[y][1] += max(fx0, fx1);
f[y][1] -= d; fs[y][1] -= d;
if ((t = f[y][0] - max(fx0, fx1) + fx0 + d) >= f[y][1])
fs[y][1] = f[y][1], f[y][1] = t;
else if (t > fs[y][1]) fs[y][1] = t;
} int ans = 0; void dfsmove(int x, int fa) {
ans = max(ans, f[x][0]);
for (int i = point[x]; i; i = E[i].nxt) {
int v = E[i].to;
if (v == fa) continue;
move(x, v, E[i].w);
dfsmove(v, x);
}
} int main() {
scanf("%d", &n);
int u, v, e;
for (int i = 1; i < n; ++i) {
scanf("%d%d%d", &u, &v, &e);
ins(u, v, e);
ins(v, u, e);
} dfs(1, 0);
dfsmove(1, 0);
printf("%d\n", ans); return 0;
}

【UOJ #105】【APIO2014】Beads and wires的更多相关文章

  1. 【UOJ#32】【UR #2】跳蚤公路(最短路)

    [UOJ#32][UR #2]跳蚤公路(最短路) 题面 UOJ 题解 不难发现要求的就是是否存在负环.也就是我们只需要找到所有的负的简单环,很容易就可以想到维护路径上和\(x\)相关的内容,即维护一下 ...

  2. 【UOJ】67 新年的毒瘤 &【BZOJ】1123 BLO

    [UOJ 67] 题目链接: 传送门 题解: 第一眼很懵逼……这什么鬼. 思考什么点复合条件……(o(>﹏<)o 1.树,也就是说还剩n-2条边,等价于要删去一个度数为m-n+2的点. 2 ...

  3. 【UOJ#236】[IOI2016]railroad(欧拉回路,最小生成树)

    [UOJ#236][IOI2016]railroad(欧拉回路,最小生成树) 题面 UOJ 题解 把速度看成点,给定的路段看成边,那么现在就有了若干边,然后现在要补上若干边,以及一条\([inf,\) ...

  4. 【UOJ#177】欧拉回路

    [UOJ#177]欧拉回路 题面 UOJ 题解 首先图不连通就没啥好搞的了. 对于无向图而言,每个点度数为偶数. 对于有向图而言,每个点入度等于出度. 然后就是一本通上有的做法,直接\(dfs\)一遍 ...

  5. 【UOJ#311】【UNR #2】积劳成疾(动态规划)

    [UOJ#311][UNR #2]积劳成疾(动态规划) UOJ Solution 考虑最大值分治解决问题.每次枚举最大值所在的位置,强制不能跨过最大值,左右此时不会影响,可以分开考虑. 那么设\(f[ ...

  6. 【UOJ#450】【集训队作业2018】复读机(生成函数,单位根反演)

    [UOJ#450][集训队作业2018]复读机(生成函数,单位根反演) 题面 UOJ 题解 似乎是\(\mbox{Anson}\)爷的题. \(d=1\)的时候,随便怎么都行,答案就是\(k^n\). ...

  7. 【UOJ#246】套路(动态规划)

    [UOJ#246]套路(动态规划) 题面 UOJ 题解 假如答案的选择的区间长度很小,我们可以做一个暴力\(dp\)计算\(s(l,r)\),即\(s(l,r)=min(s(l+1,r),s(l,r- ...

  8. 【UOJ#340】【清华集训2017】小 Y 和恐怖的奴隶主(矩阵快速幂,动态规划)

    [UOJ#340][清华集训2017]小 Y 和恐怖的奴隶主(矩阵快速幂,动态规划) 题面 UOJ 洛谷 题解 考虑如何暴力\(dp\). 设\(f[i][a][b][c]\)表示当前到了第\(i\) ...

  9. 【UOJ#422】【集训队作业2018】小Z的礼物(min-max容斥,轮廓线dp)

    [UOJ#422][集训队作业2018]小Z的礼物(min-max容斥,轮廓线dp) 题面 UOJ 题解 毒瘤xzy,怎么能搬这种题当做WC模拟题QwQ 一开始开错题了,根本就不会做. 后来发现是每次 ...

随机推荐

  1. 关于jQuery UI 使用心得及技巧

    1 jQuery UI 有时你仅仅是为了实现一个渐变的动画效果而不得不把javascrip 重新学习一遍然后书写大量代码.直到jQuery的出现,让开发人员从一大堆繁琐的js代码中解脱,取而代之几行j ...

  2. 用java代码在创建hbase表时指定region的范围

    package com.liveyc.common.utils; import java.util.List; import org.apache.hadoop.hbase.util.Bytes; i ...

  3. Intersection(HDU5120 + 圆交面积)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5120 题目: 题意: 求两个圆环相交的面积. 思路: 两个大圆面积交-2×大圆与小圆面积交+两小圆面 ...

  4. HDU 1711 Number Sequence (字符串处理 KMP)

    题目链接 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...

  5. 网络抓包wireshark(转)

    下载地址:https://www.wireshark.org/download/win64/   抓包应该是每个技术人员掌握的基础知识,无论是技术支持运维人员或者是研发,多少都会遇到要抓包的情况,用过 ...

  6. HttpURLConnection传json

    private static String sendToWangTing(DataRow dataRow) throws IOException{ String ip = Configuration. ...

  7. springMVC中ajax的实现

    function addDebtResult(){ var repayIds=$("#repayIds").val(); var lateFeeDay=$("#repay ...

  8. bash: composer: command not found

    下载composer到本地:curl -sS https://getcomposer.org/installer | php 移动至系统服务:sudo mv composer.phar /usr/bi ...

  9. 美化的select下拉框

    ie7浏览器以后的下拉框,给他加上边框样式,是没用的.要是想做出样式好看的下拉框需要用js或者jquery来模拟实现. 代码如下: <div class="r"> &l ...

  10. mknod命令

    mknod - make block or character special filesmknod [OPTION]... NAME TYPE [MAJOR MINOR]    option 有用的 ...