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$的点对要么经过根节点,要么 ...
随机推荐
- 用 querySelectorAll 来查询 DOM 节点
用 querySelectorAll 来查询 DOM 节点 Document.querySelectorAll - Web API 接口 | MDN https://developer.mozilla ...
- CLI和CGI的区别
CGI :“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上.以CGI方式运行时,web s ...
- SD/MMC异同
该文章转自:http://www.imhan.com/archives/12/ 经常看到SD/MMC这样的写法,在这里稍微总结一下SD卡和MMC卡的异同点吧. 首先,两者在外型的规格上是几乎一致的.而 ...
- centreon问题总结
1.SNMP TABLE ERROR : Requested table is empty or does not exist 这是SNMP的服务端查询客服端失败,失败的原理是权限不足 解决办法: v ...
- python selenium实现百度搜索
1.环境 python2.7+selenium+phantomjs+linux 2.代码 #-*-coding:utf-8 -*- from selenium import webdriver fro ...
- alsa和oss声音系统比较
OSS(Open Sound System) OSS的含义为,Open Sound System,是unix平台上一个统一的音频接口.以前,每个Unix厂商都会提供一个自己专有的API,用来处理音频. ...
- bzoj3136: [Baltic2013]brunhilda
这个题为什么会放在数据结构啊 首先因为有决策包容性,对于一个n每次必然选择一个n%p最大的p,令n减n%p 设fi表示i变成0的步数的话,同样我们可以知道f是有单调性的 假如fd能转移到fk,首先d一 ...
- SPOJ:Eagle and Dogs(求树上每个点最远可以走到哪里---树的直径||DP)
Eagle (AKA Mohamed Ahmed) lives in a city consists of n intersections connected by n-1 roads, in a w ...
- [SoapUI] Jenkins 配置
cd %WORKSPACE% cmd /c call "%SOAPUI_PRO_HOME%\bin\testrunner.bat" -a -j -PprojectPath=&quo ...
- Watir: Win32ole对于excel某些指令无法操作的时候有如下解决方案
Similar Threads 1. WIN32OLE - failed to create WIN32OLE 2. WIN32OLE#[] and WIN32OLE#[]= method in Ru ...