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的更多相关文章

  1. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  2. Codeforces 161D Distance in Tree(树型DP)

    题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...

  3. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  4. CF161D Distance in Tree

    CF161D Distance in Tree LG传送门 长链剖分板子题. 长链剖分那么好写,跑得又快,为什么要写点分治呢?完了我现在看一道点分治题就想写长链剖分 如果还不会长链剖分请看我博客. 没 ...

  5. 【树形dp】Distance in Tree

    [CF161.D] Distance in Tree time limit per test 3 seconds memory limit per test 512 megabytes A tree  ...

  6. 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 ...

  7. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  8. Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset

    Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...

  9. codeforces 161D Distance in Tree 树上点分治

    链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...

随机推荐

  1. linux进程间通信消息队列:msgsnd: Invalid argument

    今天写了个消息队列的小测试程序结果send端程序总是出现:msgsnd: Invalid argument,搞了半个小时也没搞明白,后来查资料发现我将(st_msg_buf.msg_type = 0; ...

  2. Arcgis Engine(ae)接口详解(8):临时元素(element)

    //主地图的地图(map)对象 IMap map = null; IActiveView activeView = null; //IGraphicsContainer用于操作临时元素,可以通过map ...

  3. 在MVC中使用泛型仓储模式和工作单元来进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  4. GDI泄露+改EXE名

    CDC 应该是成对使用 GetDC and ReleaseDC(不用new and delete) 泄露 1.改变生产exe名称:工程->设置->连接->输出文件名:Release/ ...

  5. Bootstrap progress-bar

    1.进度条 在网页中,进度条的效果并不少见,比如一个评分系统,比如加载状态等.就如下图所示的一个评分系统,他就是一个简单的进度条效果: 进度条和其他独立组件一样,开发者可以根据自己的需要,选择对应的版 ...

  6. Maze 解题报告

    Maze Description   You are given a special Maze described as an n*m matrix, please find the shortest ...

  7. html5--6-59 其他常用CSS属性

    html5--6-59 其他常用CSS属性 实例 学习要点 了解opacity属性:透明度设定 了解cursor属性:自定义鼠标样式 了解CSS新单位rem和em的区别 了解轮廓outline的设置 ...

  8. Python学习笔记_Mysql数据库、Excel

    一.操作mysql数据库 import pymysql # 1.连上数据库:账号,密码,ip,端口号,数据库 # 2.建立游标(去数据库拿东西的工人) # 3.执行sql # 4.获取结果 # 5.关 ...

  9. MongoDB C++ gridfs worked example

    使用libmongoc,参考:http://mongoc.org/libmongoc/current/mongoc_gridfs_t.html #include <mongoc.h> #i ...

  10. 禁止屏幕旋转并同时解决以至于导致Activity重启的方法

    1.禁止屏幕旋转在AndroidManifest.xml的每一个需要禁止转向的Activity配置中加入android:screenOrientation属性. //landscape(横向)port ...