
树形DP
brucehb
这个作者很懒,什么都没留下…
展开
-
51nod - 1424 零树
有一棵以1为根的树,他有n个结点,用1到n编号。第i号点有一个值vi。现在可以对树进行如下操作:步骤1:在树中选一个连通块,这个连通块必须包含1这个结点。步骤2:然后对这个连通块中所有结点的值加1或者减1。问最少要经过几次操作才能把树中所有结点都变成0。注意:步骤1与步骤2合在一起为一次操作。Input单组测试数据。第一行有一个整数n(1≤n≤10^5...原创 2018-03-10 22:31:57 · 819 阅读 · 0 评论 -
Poj - 3107 Godfather
DescriptionLast 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 arrest the mafia leaders.Unfortunately, ...原创 2018-07-30 23:25:27 · 295 阅读 · 0 评论 -
HDU - 5909 Tree Cutting
DescriptionAfter Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible cost, he sued Bessie to mitigate his losses.Bessi...原创 2018-07-31 00:21:18 · 199 阅读 · 0 评论 -
POJ-1947 Rebuilding Roads
DescriptionThe cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn't have time to rebuild any extra ...原创 2018-08-10 21:57:09 · 253 阅读 · 0 评论 -
POJ - 2486 Apple Tree
DescriptionWshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip a...原创 2018-08-02 00:42:30 · 144 阅读 · 0 评论 -
选课
【问题描述】学校实行学分制。每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分。 学校开设了 N(N<300)门的选修课程,每个学生可选课程的数量 M 是给定的。学生选修了这 M 门课并考核通过就能获得相应的学分。 在选修课程中,有些课程可以直接选修,有些课程需要一定的基础知识,必须在选了其它的一些课程的基础上才能选修。例如《Frontpage》必须在选修了《Windows 操...原创 2018-08-12 00:20:05 · 684 阅读 · 2 评论 -
HDU - 1520 Anniversary party
DescriptionThere is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relati...原创 2018-08-12 13:59:18 · 215 阅读 · 0 评论 -
HDU - 4714 Tree2cycle
Problem DescriptionA tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 unit of cost respectively. The nodes are labeled from 1 to N. Your job is to transform the...原创 2018-08-15 21:13:13 · 323 阅读 · 0 评论 -
Starship Troopers
Problem DescriptionYou, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected w...原创 2018-08-16 01:23:08 · 384 阅读 · 0 评论 -
TELE
DescriptionA TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tree is a transmitter that emits the foo...原创 2018-08-14 01:18:54 · 480 阅读 · 0 评论 -
51nod - 1677 treecnt
给定一棵n个节点的树,从1到n标号。选择k个点,你需要选择一些边使得这k个点通过选择的边联通,目标是使得选择的边数最少。现需要计算对于所有选择k个点的情况最小选择边数的总和为多少。样例解释:一共有三种可能:(下列配图蓝色点表示选择的点,红色边表示最优方案中的边)选择点{1,2}:至少要选择第一条边使得1和2联通。选择点{1,3}:至少要选择第二条边使得1和3联通。...原创 2018-08-19 15:43:19 · 470 阅读 · 0 评论 -
51nod - 1314 定位系统
一个国家有N个城市(标号为0~N-1),这N个城市恰好由N-1条道路连接在一起(即N个城市正好构成一个树状结构)。这个国家的所有道路的长度都是1个长度单位。定义:两个城市间的距离是两个城市间的最短路的长度。现在这个国家想建立一套定位系统,让国家的公民能通过这套系统定位自己所在的城市。该系统由K个有编号的信号站构成,不妨将它们标号为0,1,2,3,...,K-1。每个信号站会放在一个城市中,每个...原创 2018-07-30 02:03:17 · 702 阅读 · 0 评论 -
51nod - 1405 树的距离之和
给定一棵无根树,假设它有n个节点,节点编号从1到n, 求任意两点之间的距离(最短路径)之和。Input第一行包含一个正整数n(n<=100000),表示节点个数。后面(n-1)行,每行两个整数表示树的边。Output每行一个整数,第i(i=1,2,...n)行表示所有节点到第i个点的距离之和。Input示例4123242Outpu...原创 2018-07-29 15:31:43 · 300 阅读 · 0 评论 -
51nod - 1378 夹克老爷的愤怒
夹克老爷逢三抽一之后,由于采用了新师爷的策略,乡民们叫苦不堪,开始组织起来暴力抗租。夹克老爷很愤怒,他决定派家丁常驻村中进行镇压。诺德县 有N个村庄,编号0 至 N-1,这些村庄之间用N - 1条道路连接起来。家丁都是经过系统训练的暴力机器,每名家丁可以被派驻在一个村庄,并镇压当前村庄以及距离该村庄不超过K段道路的村庄。夹克老爷一贯奉行最小成本最大利润的原则,请问要实现对全部村庄的...原创 2018-04-16 14:19:21 · 224 阅读 · 0 评论 -
旅行计划
某个国家有N个城市,编号0 至 N-1,他们之间用N - 1条道路连接,道路是双向行驶的,沿着道路你可以到达任何一个城市。你有一个旅行计划,这个计划是从编号K的城市出发,每天到达一个你没有去过的城市,并且旅途中经过的没有去过的城市尽可能的多(如果有2条路线,经过的没有去过的城市同样多,优先考虑编号最小的城市),直到所有城市都观光过一遍。现在给出城市之间的交通图T,以及出发地点K,你来设计一个旅行计...原创 2018-05-01 00:11:26 · 799 阅读 · 0 评论 -
连通率
A国拥有n个城市,其交通系统呈树状结构,即任意两个城市存在且仅存在一条路径将其连接。B国是A国的敌国企图秘密发射导弹打击A国,由于情报有限A国的特工们并不知道有哪几座城市会被打击。一座城市被炸毁后,与其相连的交通线也将损毁。现假设每座城市都有50%的概率被炸毁,问B国发动袭击后A国期望有多少对城市仍连通。 样例解释:城市{1,2,3}被炸毁:没有城市连通城市{1,2}被炸毁:没有城...原创 2018-06-21 23:12:45 · 1379 阅读 · 0 评论 -
关于树的函数
有两颗无根树Tree1与Tree2,这两棵树各有N个节点(2<=N<=4000)。每棵树中,N个节点都被标为0,1,2,..,N-1,这N个序号,不保证两颗树是同构的。定义一个关于这两棵树的函数S(e1,e2),其中e1为Tree1的一条边,e2为Tree2的一条边,定义如下:(1)如果将边e1从Tree1中移除(即Tree1在e1处断开),那么Tree1将分裂为两棵小树A1与B1;(...原创 2018-07-08 17:28:46 · 1217 阅读 · 0 评论 -
彩色树
思路:树形dp。先计算出不经过某种颜色的点对数量,再用colNum*n*(n-1)减去这些点对数量即可。#include <iostream>#include <cstdio>using namespace std; typedef long long ll;const int MAXN = 1e5 + 5;const int MOD =...原创 2018-06-27 01:11:50 · 319 阅读 · 0 评论 -
POJ - 1655 Balancing Act (求树的重心)
DescriptionConsider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to...原创 2018-07-25 22:32:33 · 368 阅读 · 0 评论 -
洛谷P1352 没有上司的舞会
题目描述某大学有N个职员,编号为1~N。他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司。现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri,但是呢,如果某个职员的上司来参加舞会了,那么这个职员就无论如何也不肯来参加舞会了。所以,请你编程计算,邀请哪些职员可以使快乐指数最大,求最大的快乐指数。输入输出格式输入格式: 第一行...原创 2018-07-26 00:07:53 · 164 阅读 · 0 评论 -
HDU-4003 Find Metal Mineral
Problem DescriptionHumans have discovered a kind of new metal mineral on Mars which are distributed in point‐like with paths connecting each of them which formed a tree. Now Humans launches k robots...原创 2018-08-17 01:09:16 · 306 阅读 · 0 评论