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$的点对要么经过根节点,要么 ...
随机推荐
- hiho1079 线段树区间改动离散化
题目链接: hihocoder1079 代码: #include<iostream> #include<cstdio> #include<cstring> #inc ...
- 基于PHP函数的alert弹框
可以设置弹出信息,跳转地址,跳转的时间,跳转的信息标题提示: 手机端加上<meta name='viewport' content='width=device-width, initial-sc ...
- 2016/06/16 phpexcel
程序部分 require_once './phpexcel/PHPExcel.php'; // 首先创建一个新的对象 PHPExcel object $objPHPExcel = new ...
- gradle配置
一.你不想看到的 Gradle Build Running 话说在天朝当程序员也是很不容易的,不管是查阅资料还是下载东西,很多时候你会发现自己上网姿势不对,当然对大多数程序员来说,这都不是事儿.这次重 ...
- 恢复MySQL数据库删除的数据
在日常运维工作中,对于数据库的备份是至关重要的!数据库对于网站的重要性使得我们对 MySQL 数据库的管理不容有失!然而是人总难免会犯错误,说不定哪天大脑短路了,误操作把数据库给删除了,怎么办? 下面 ...
- 数据结构之 栈与队列--- 走迷宫(深度搜索dfs)
走迷宫 Time Limit: 1000MS Memory limit: 65536K 题目描述 一个由n * m 个格子组成的迷宫,起点是(1, 1), 终点是(n, m),每次可以向上下左右四个方 ...
- linq to xml There are multiple root elements.
添加xml结点的时候 var temp2 = temp1.Element("staticContent"); if (temp2 != null) { string str = & ...
- java中Math常用方法
public class Demo{ public static void main(String args[]){ /** *Math.sqrt()//计算平方根 *Math.cbrt()//计算立 ...
- 使用gcc找出头文件的路径
参考 http://stackoverflow.com/questions/13079650/how-can-i-find-the-header-files-of-the-c-programming- ...
- bzoj2973石头游戏——矩阵乘法
题目:权限题! 写了一下,但提交不了,先放着吧. 代码如下: #include<iostream> #include<cstdio> #include<cstring&g ...