题目链接 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…
题目大概是,给一棵树,统计距离为k的点对数. 不会DP啊..点分治的思路比较直观,啪啪啪敲完然后AC了.具体来说是这样的: 树上任何两点的路径都可以看成是一条过某棵子树根的路径,即任何一条路径都可以由一个子树到达根的一条或两条路径组成 就可以分治累加各个结点为根的子树的统计数目 对于各个子树可以这样统计:假设这个子树的根有a.b.c...若干个孩子,开一个数组cnt[i]记录有几个结点到根结点为i,依次处理a.b.c...结点及各自以下的结点,处理的时候根据当前的cnt数组统计数目,处理完后把新…
链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么跨过子树的根节点,于是考虑树分治 用类似poj1741的想法,可以推出: 对于任意一棵子树,其根节点记为$C$,其子树中: 记距离$C$距离之和为$k$的点对数量$S_{c}$ 记$C$儿子节点$C_1...C_n$的子树中,距离$C_i$距离为$k-2$的点对数量为$S'_{c_i}$ 其符合条件…
题目大意:给出一棵n个节点的树,统计树中长度为k的路径的条数(1<=n<=50000 , 1<=k<=500) 思路:树分治! #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #define ll long long ],F[],sum; ll ans; ],pd[],vis[],A[]; ]…
<题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N int(5e4+10) int n,k,ans,cnt; ],head[N]; struct Edge{ int to,nxt; }edge[N<<];…
题目给了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>…
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…
题目描述 Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k…
D Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 1687    Accepted Submission(s): 263 Problem Description There is a skyscraping tree standing on the playground of Nanjing University of…