CF161D Distance in Tree LG传送门 长链剖分板子题. 长链剖分那么好写,跑得又快,为什么要写点分治呢?完了我现在看一道点分治题就想写长链剖分 如果还不会长链剖分请看我博客. 没什么好说的,时空复杂度\(O(n)\)直接洛谷rank1. #include<cstdio> #include<cctype> #define R register #define I inline using namespace std; const int S=50003,N=10…
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…
Problem Distance in tree 题目大意 给出一棵树,求这棵树上有多少个最短距离为k的点对. Solution 这个题目可以用点分治来做,然而我到现在还是没有学会点分治,所以只好用树形dp了. 这个题目,我们可以将其转化为一个个子树中搞事情,再慢慢合并. 设f[i][j]为以i为根的子树中距离根距离为j的点有多少个. 对于一个点u,我们枚举它的子节点v,则我们可以计算出经过u-v这条边的答案 我们枚举j=1->k,则ans+=f[u][j]*f[v][k-j-1]; 枚举完以后…
题目链接: 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…
题目链接 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…