【树形dp】Distance in Tree】的更多相关文章

[poj2486]Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10800   Accepted: 3629 Description Wshxzt 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 ha…
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsmemory limit per test 512 megabytes 问题描述 A tree is a connected graph that doesn't contain any cycles. The distance between two vertices of a tree is th…
[CF161.D] Distance in Tree time limit per test 3 seconds memory limit per test 512 megabytes A tree is a connected graph that doesn't contain any cycles. The distance between two vertices of a tree is the length (in edges) of the shortest path betwee…
T1题面: 输入点数为N一棵树 求树上长度恰好为K的路径个数 (n < 1e5, k < 500) 这是今天的考试题,也是一道假的紫题,因为我一个根本不会dp的蒟蒻只知道状态就一遍A掉了--(然后我当时不会--emm) 考虑f[i][j]表示点i为根的子树中深度为j的点的个数,初始设置f[i][0] = 1.转移的时候,每搞完一棵子树就用这棵子树内的数据用乘法原理更新ans,然后再把它的贡献累加给根,这样可以保证统计不重不漏. 也可以用点分治来做. 代码: #include <iostr…
题目:http://codeforces.com/problemset/problem/161/D 题意:给你一棵树,问你两点之间的距离正好等于k的有多少个 思路:这个题目的内存限制首先大一倍,他有5*1e5个点,k的范围是<=500,首先暴力n^2肯定不行,这个题其实很容易看出是树形dp 首先k的范围只有500,我们可以开一个dp[1e5][500]的dp数组,代表以这个点为上端点的所有情况,然后最后递归到1点处,dp[1][k]就是答案 #include<cstdio> #inclu…
题意:求树上距离为k的点对个数: 解题关键:练习一下点分治不用容斥 而直接做的做法.注意先查询,后更新. 不过这个方法有个缺陷,每次以一个新节点为根,必须memset mp数组,或许使用map会好些,更新序号一类用ca这种形式更好些. 试了一下,map更慢,应该是带log的原因. 点分治解法: #pragma comment(linker,"/STACK:102400000,102400000") #include<cstdio> #include<cstring&g…
题目链接:http://codeforces.com/problemset/problem/161/D 题意:给出一个树,问树上点到点的距离为k的一共有几个. 一道简单的树形dp,算是一个基础题. 设dp[i][len]表示i为根距离为len的一共有几个点. 一般的树形dp都是先dfs然后再更新dp的值,注意按这样写就行了.而且一般的树形dp都是设dp[i][k]i为根,k为条件. void dfs(int u , int pre) { int len = vc[u].size(); dp[u]…
传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tree consisting exactly of nn vertices. Tree is a…
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also has a migic tree, the tree has N nodes , in each node , there is a treasure, it's value is V[i], and for each edge, there is a cost C[i], which means…
题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1037    Accepted Submission(s): 298 Problem Description Bi Luo is a magic boy, he also has a migic…