Codeforces 161 D. Distance in Tree (树dp)】的更多相关文章

题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点个数,2次dfs,一次从底向上,另一次从顶向下. //#pragma comment(linker, "/STACK:102400000, 102400000") #include <algorithm> #include <iostream> #include &…
题目链接: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/219/problem/D 树dp //#pragma comment(linker, "/STACK:102400000, 102400000") #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include…
题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息由他的儿子转移过来. 那么一边进行状态转移,一边统计答案即可. #include <bits/stdc++.h> using namespace std; ][], deep[]; vector <]; int n, k, x, y; long long ans; void dfs(int…
题目链接: 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…
Prime Distance On Tree Problem description. You are given a tree. If we select 2 distinct nodes uniformly at random, what's the probability that the distance between these 2 nodes is a prime number? Input The first line contains a number N: the numbe…
D. Distance in Tree time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output A tree is a connected graph that doesn't contain any cycles. The distance between two vertices of a tree is the length (…
题意 你有一棵 \(n\) 个点的树,每次会随机选择树上的一条边,将两个端点 \(u,v\) 合并,新编号随机为 \(u,v\).问最后保留的编号分别为 \(1\) 到 \(n\) 的概率. \(n\leq 50\) . 分析 考虑枚举钦定一个编号为 \(ans\) 之后以他为根跑一次树dp. 思考一下操作的执行过程.首先,操作连接 \(rt\) 和他的儿子 \(v\) 的边时,必须保留 \(rt\) 的编号,然后合并掉 \(v\) ,可以看成是 \(rt\) 将他的编号传递给了 \(v\) (…
题目给了512MB的空间....用dp[k][i]代表以k为起点...往下面走(走直的不打岔)i步能有多少方案....在更新dp[k][i]过程中同时统计答案.. Program: #include<iostream> #include<queue> #include<stack> #include<stdio.h> #include<string.h> #include<algorithm> #include<cmath>…
\(其实思路都能想到一点,就是去重这里特别麻烦,没有好的思路.\) \(设dp[i][j]为以i为根深度为j的节点数量\) \(dp[parent][j]=\sum{dp[son][j-1]}\) \(然后把每个节点作为转折点求答案\) #include <bits/stdc++.h> using namespace std; const int maxn=50009; struct p{ int to,nxt; }d[maxn*2]; int n,k,deep[maxn],head[maxn…