ZOJ 3949 Edge to the Root( 树形dp)】的更多相关文章

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 题解:树dp真的很直觉,或者说dp真的很直觉.就上周末比赛时其实前一半的观察我都找到了,也感觉就是O(N)的,但就是不会转移.观察其实就是说这条边会使得x这个子树的距离变少,以及从1到x这条链的中点+1的这个点的子树的距离变少.那么怎么从fa(x)转移过来呢.对于dep[x]/2+1的子树,因为x相对于fa(x)变深了,所以子树内的每一个都要+1,然而对于x的子树中…
Edge to the Root Time Limit: 1 Second      Memory Limit: 131072 KB Given a tree with n vertices, we want to add an edge between vertex 1 and vertex x, so that the sum of d(1, v) for all vertices v in the tree is minimized, where d(u, v) is the minimu…
[题目链接] http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3949 [题目大意] 给出一棵根为1的树,每条边边长为1,请你从根连一条边到某个点, 使得各点到根距离的总和最小,求这个最小距离和 [题解] 假设从1连到x,那么受影响的只有1和x中点往下的部分, 我们发现中点往下的部分根据每个点答案改变的大小可以分为多个部分, 每个部分大小为其1-x链上的父节点子树大小减去其链上第一子节点子树大小, 设其链上父节点为y,那么…
题意: 在一棵树中,可以从根节点往其他节点加一条边,使得根节点到其他所有节点的距离和最小,输出最小的距离和. 思路: 我们考虑在加的一条边为$1 \to v$,那么在树上从$1 \to v$的路径上,如果有一个点$y$到$v$比到$1$更近,那么这个点$y$的子树里的所有 点都到$v$更近.那么我们找到离根最近的点$y$,那么$y$子树中的所有点都是到$v$更近. 我们考虑: $f[u]$表示如果添加了$1 \to u$这条边的最小距离和是多少. $g[u]$表示如果添加了$1 \to u$这条…
Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since a bloodsucker living there. Sometimes the bloodsucker will appear and kill everyone who isn't at his hometown. One day, a brave person named CC finds…
题意:给一棵树,一个人站在节点s,他有m天时间去获取各个节点上的权值,并且最后需要回到起点s,经过每条边需要消耗v天,问最少能收获多少权值? 思路: 常规的,注意还得跑回原地s. //#include <bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstring> #include <map> #include <algorithm> #include…
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 题目大意:方块连接,呈树形.每个方块有两种接法,一种接在父块边上,宽度+1,一种接在父块上面,宽度+0.且一个母块最多有2个子块.问全局的宽度最小是多少. 解题思路: 对于一个方块,就两种接法. 设dp[i][0]=0,表示接在父块上面. dp[i][1]=1,表示接在父块边上. 对于一个父块,如果没有子块,宽度不变. 如果有一个子块,肯定接在上面,加上子块…
#include<cstdio> #include<vector> #include<cstring> #include<iostream> using namespace std; ; vector<int>a[MAXN]; int n,m,v[MAXN],vis[MAXN],dp[MAXN][MAXN]; void dfs(int root) { dp[root][] = v[root]; vis[root] = ; int i, len =…
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3278 题意:给出一棵树,找出一个不大于长度为m的链,使得其他点到该链的长度之和最小. 预处理出以下数组: (1)downSum[u]:u的所有孩子到达u的距离,downCnt[u]:u子树节点个数: (2)upSum[u]:除u子树的节点外其他节点到达u的距离:upCnt[u]:除u子树的节点个数. 树形DP时,竖线形状的比较简单,我们用dp[u][L]表示以u为子…
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5568 Edge to the Root Time Limit: 1 Second      Memory Limit: 131072 KB Given a tree with n vertices, we want to add an edge between vertex 1 and vertex x, so that the sum of d(1, v) for all…