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$的点对要么经过根节点,要么 ...
随机推荐
- (转载)解决MySql 数据库 提示:1045 access denied for user 'root'@'localhost' using password yes
今天想用用MySQL 数据库 谁知道老提示 1045 access denied for user 'root'@'localhost' using password yes 最后在csdn 上找到 ...
- poj 1179 $Polygon$(断环成链)
Polygon \(solution:\) upd:还是多讲一下,这道题基本上可以说是一道思维题.一道结论题.一道考验你动态规划基本功是否扎实的题目.因为这道题的数据范围很小,思考一下总能想到断环成链 ...
- TFS Server 2017 自动化部署步骤
1 第一步,在服务器上安装TFS 2 第二步,安装完TFS后需要配置你的项目,选择管理代码的方式,这里我们可以选择传统的TFS 也可以选择GIT 方式,此处我选择的GIT 方式 3 第三步,设置代理. ...
- openfire学习(一)
需要开发一款软件,其中的即时通讯功能使用xmpp协议来做,服务端用openfire. openfire的下载和配置就不多说,可能大多数人第一次用会遇到登陆不了的问题,我也遇到了,事实上登陆错误是因为用 ...
- ros使用时的注意事项&技巧2
1.查看参数列表 rosparam list 2.查询参数rosparam get parameter_name,如rosparam get /rosdistro 3.设置参数rosparam set ...
- FAT和FAT32文件系统的原理
[转自] http://www.sjhf.net/Article/sjhfdoc/200404/1.html 一.硬盘的物理结构: 硬盘存储数据是根据电.磁转换原理实现的.硬盘由一个或几个表面 ...
- linq to xml There are multiple root elements.
添加xml结点的时候 var temp2 = temp1.Element("staticContent"); if (temp2 != null) { string str = & ...
- codeforces 435 B. Pasha Maximizes 解题报告
题目链接:http://codeforces.com/problemset/problem/435/B 题目意思:给出一个最多为18位的数,可以通过对相邻两个数字进行交换,最多交换 k 次,问交换 k ...
- POJ2443 Set Operation (基础bitset应用,求交集)
You are given N sets, the i-th set (represent by S(i)) have C(i) element (Here "set" isn't ...
- RDA TDT & TOT
首先看下面的TS PSI分析图: 注意:TOT UTC与TDT是一致的 TDT下的时间为: UTC+手动TIMEZONE TOT下的时间为: UTC+解析的time_offset time_offes ...