Distance in Tree CodeForces - 161D
Distance in Tree CodeForces - 161D
题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u、v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v)/(v,u)算一对)。
方法:
ans[i][k]表示与i结点距离为k的子结点个数
ans[i][k]=sum{ans[son][k-1]}
ans[i][0]=1
sum[i]表示(u,v)都为i的子结点且(u,v)的最短路径过i点
sum[i]=sum{ans[i][p]*ans[i][k-p]}//不对,会多计同一条链上的
sum[i]=sum{ans[son][p]*sum{ans[otherson][k-p-2]}}//对,但是太慢了
sum[i]=sum{ans[son][p]*(ans[i][k-p-1]-ans[son][k-p-2])}//换种写法(自己想不到)
由于(u,v)/(v,u)算一对,所以实际上i点有关的答案(也就是一个端点为i点,或(u,v)都为i的子结点且(u,v)的最短路径过i点)为:
$ans[i][k]+sum[i]/2$
由于不需要各个点分开记,可以直接用一个ans加上这些值,不用开sum。
#include<cstdio>
#include<vector>
using namespace std;
typedef long long LL;
struct Edge
{
LL to,next;
}edge[];
LL first1[],num_edge;
LL ans[][];
LL ans1,ans2,n,k2;
bool vis[];
void dfs(LL u)
{
LL k=first1[u],i,j;
vector<LL> temp;
ans[u][]=;
vis[u]=true;
while(k!=)
{
LL &v=edge[k].to;
if(!vis[v])
{
dfs(v);
for(i=;i<=k2;i++)
ans[u][i]+=ans[v][i-];
temp.push_back(v);
}
k=edge[k].next;
}
ans2+=ans[u][k2];
for(i=;i<temp.size();i++)
for(j=;j<=k2-;j++)
ans1+=ans[temp[i]][j]*(ans[u][k2-j-]-ans[temp[i]][k2-j-]);
}
int main()
{
LL i,a,b;
scanf("%I64d%I64d",&n,&k2);
for(i=;i<n;i++)
{
scanf("%I64d%I64d",&a,&b);
edge[++num_edge].to=b;
edge[num_edge].next=first1[a];
first1[a]=num_edge;
edge[++num_edge].to=a;
edge[num_edge].next=first1[b];
first1[b]=num_edge;
}
dfs();
printf("%I64d",ans2+ans1/);
return ;
}
Distance in Tree CodeForces - 161D的更多相关文章
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- Codeforces 161D Distance in Tree(树型DP)
题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- CF161D Distance in Tree
CF161D Distance in Tree LG传送门 长链剖分板子题. 长链剖分那么好写,跑得又快,为什么要写点分治呢?完了我现在看一道点分治题就想写长链剖分 如果还不会长链剖分请看我博客. 没 ...
- 【树形dp】Distance in Tree
[CF161.D] Distance in Tree time limit per test 3 seconds memory limit per test 512 megabytes A tree ...
- CodeChef - PRIMEDST Prime Distance On Tree 树分治 + FFT
Prime Distance On Tree Problem description. You are given a tree. If we select 2 distinct nodes unif ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
- codeforces 161D Distance in Tree 树上点分治
链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...
随机推荐
- 【BZOJ】1007 水平可见直线
[分析] 维护一个下凸包. 首先依照斜率来从小到大排序. 考虑斜率同样的,肯定仅仅能选截距大的,把截距小的给筛掉. 然后用栈来维护下凸包.先压入前两条直线. 然后对于每一条直线i,设栈中上一条直线p= ...
- Java虚拟机平台无关性
jruby Java 虚拟机面试题全面解析(干货) - Yano_nankai的博客 - CSDN博客 http://m.blog.csdn.net/Yano_nankai/article/detai ...
- python 1: 解决linux系统下python中的matplotlib模块内的pyplot输出图片不能显示中文的问题
问题: 我在ubuntu14.04下用python中的matplotlib模块内的pyplot输出图片不能显示中文,怎么解决呢? 解决: 1.指定默认编码为UTF-8: 在python代码开头加入如下 ...
- 命令行唤起xcode模拟器
1.IOS模拟器列表获取命令 xcrun instruments -s 2.IOS启动模拟器命令 xcrun instruments -w "iPhone 8 (12.1)"
- date format记录
各种日期格式定义,容易忘记,这里备注下: * 支持格式为 yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD at 22:10:59 PSD'<br> ...
- SDUT OJ 1221 亲和数 (找出某个数n所有的因子数,只需要暴力:2->sqrt(n) 即可 )
亲和数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 如果a的因子和等于b,b的因子和等于a,且a≠b,则称a,b为亲和数对. ...
- java socket 以及 流 关闭的问题
首先我一下几个提出问题:稍后再做出解答. 问题一:A如果仅仅将输入流关闭(inA.close()),对A与B之间的连接是否有影响? A能否再次获得输入流(inA = socketA.getInputS ...
- Android隐藏Activity和图标
今天发现4.0以后如果不写Activity只写BroadcastReceiver的话,这个广播接收器是不能运行的.经过查询,好像是HoneyComb之后添加了安全机制,规定必须运行一次Activity ...
- [Selenium] common functions comparison
1.Wait for element in default time or self defined time When the element need some time to be prese ...
- Ski Course Design
链接 分析:读题!读题!读题!重要的事说三遍,中文翻译漏掉了一个重要的地方,每个只能用一次,调了一下午还以为标程错了,其实就是找一段长为17的区间,然后使所有都处于这个区间中代价最小,暴力枚举即可. ...