ACdream1032 Component 树形DP】的更多相关文章

思路:dp[i][j]表示以i为根结点有j个连通节点的最小和, 当进行状态转移时需要利用01背包,节点u下面有多个子节点,每个子节点可以最多可以贡献cnt[v]个节点,cnt[v]表示以v为根结点的树的节点总数. AC代码 #include <cstdio> #include <cmath> #include <cctype> #include <algorithm> #include <cstring> #include <utility…
ACdream1032 题意 给出一棵树,每个节点有权值,问由 \(1\) ~ \(n\) 个节点组成的树块的权值和的最小值. 分析 首先发现 \(n\) 很小,那么我们可以开一个二维数组 \(dp[i][j]\) 表示以 \(i\) 节点为树块的根节点且树块中节点的数量为 \(j\) 时的最小值. 从 \(u\) 节点出发,先去遍历子树,再用子树的那个根节点 \(v\) 去更新 \(u\) 的值. code #include<cstdio> #include<cstring> #…
题目链接: E. Centroids time limit per test 4 seconds memory limit per test 512 megabytes input standard input output standard output Tree is a connected acyclic graph. Suppose you are given a tree consisting of n vertices. The vertex of this tree is call…
Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7536   Accepted: 2659 Description Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to…
Accumulation Degree Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3425   Accepted: 859 题目链接:http://poj.org/problem?id=3585 Description: Trees are an important component of the natural landscape because of their prevention of erosion an…
Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Description Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has just received a bad news which denotes that DxtNet…
可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话dp貌似就比较麻烦了. 我们考虑一般树形dp都是怎样的,一般的树形dp,都是因为子树上的f值可以无后效的转移到根节点上,并且子树的f值与父亲无关,如果我们按照上述定义,那么就会发现这需要两遍dfs来解决,并且细节不少. 但是两遍dfs我并不会QAQ 所以我们考虑转换一种定义,设f[x][k]表示在以x…
4726: [POI2017]Sabota? Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 128  Solved: 49[Submit][Status][Discuss] Description 某个公司有n个人, 上下级关系构成了一个有根树.其中有个人是叛徒(这个人不知道是谁).对于一个人, 如果他下属(直接或者间接, 不包括他自己)中叛徒占的比例超过x,那么这个人也会变成叛徒,并且他的所有下属都会变成叛徒…
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不考虑i点时子树的最优权值和,即(j是i的儿子),显然dp[i]>=sum[i].那么问题是考虑i点时dp[i]的值是多少,假设有一条链通过i,且端点a和b都在i的子树里,即LCA(a,b)=i,如果考虑加上这条链的权值,那么a->i, b->i的路上的点v都不能有链经过它们(题目要求链不相交…
切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <map> #include <queue> #include <set>…