function<void(int,int)> dfs = [&](intu,intp){ dp[u][0] =1;for(constint& v : al[u]){if(v != p){dfs(v, u);for(inti =0; i < k; ++i){ ans += dp[u][i] * dp[v][k - i -1]; }for(inti =1; i < k; ++i){ dp[u][i] += dp[v][i -1]; } } } }...
codeforces 161 D. Distance in Tree(树形dp) 题目链接:http://codeforces.com/problemset/problem/161/D 题意:给出一个树,问树上点到点的距离为k的一共有几个。 一道简单的树形dp,算是一个基础题。 设dp[i][len]表示i为根距离为len的一共有几个点。 一般的树形dp都是先dfs然后再更新dp的值,注意按...
public class Main { static InputReader in; static PrintWriter out; public static void main(String[] args) throws IOException{ //InputStream ins = new FileInputStream("E:\\rush.txt"); InputStream ins = System.in; in = new InputReader(ins); out = new PrintWriter(System.out); //code s...
Hello guys, I was solving this problem (http://codeforces.com/contest/161/problem/D). After trying for sometime , I read the editorial of the problem and I am not being able to understand the meaning of this line. Can anyobdy help me with this? I basically want to understand about ...
[A - Distance in Tree] 树形dp A - Distance in Tree CodeForces - 161D 题目大意:树是一个不包含任何圈的连通图。树的两个节点之间的距离是节点之间最短路径的长度(也就是边的长度)。 给定一棵有n个节点的树和一个正整数k,找出距离恰好为k的不同节点对的数量。注意,节点对(v, u)和节点对(u, v)...
Mangooste → XIX Open Olympiad in Informatics (Unrated, Online Mirror, IOI rules) gatapdev → Is there any case where the result is 0 in 2070D? KAN → Codeforces Blitz Cup 2025: final rounds 123gjweq2 → Why are there so many unrated users joining the next contest? _U_Turn_ → ...
This version of the problem differs from the previous one only in the constraint on nn. A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The distance between two vertices is the minimum sum of weights on the path connecting them. ...
For example, given the following tree: The minimum distance is 1 and can be obtained in multiple ways, but one of them is: And the maximum distance is 4 and can be obtained like this: I couldn't find anything like this by googling so I thought this is the best place to ask. Thank...
CodeForces - 1108 F MST Unification【MST最小生成树】 only of edges of the given graph. A minimum spanning tree (MST) in case of positive weights is a subset... distance between vertices uiui and vivi along the edge eiei is wiwi (1≤wi1≤wi). The graph is MachineLearning笔记week1 ...
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 between these vertices. You are given a tree with n vertices and a positive number k. Find the number of distinct pairs of the vertic...