CF161D Distance in Tree

UPD:憨憨博客 公开处刑

LG传送门

长链剖分板子题。

长链剖分那么好写,跑得又快,为什么要写点分治呢?完了我现在看一道点分治题就想写长链剖分

如果还不会长链剖分请看我博客

没什么好说的,时空复杂度\(O(n)\)直接洛谷rank1。

#include<cstdio>
#include<cctype>
#define R register
#define I inline
using namespace std;
const int S=50003,N=100003;
char buf[1000000],*p1,*p2;
I char gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,S,stdin),p1==p2)?EOF:*p1++;}
I int rd(){
R int f=0; R char c=gc();
while(c<48||c>57) c=gc();
while(c>47&&c<58) f=f*10+(c^48),c=gc();
return f;
}
int h[S],s[N],g[N],p[S],q[S],d[S],t[S],*f[S],u[S],c,K,*e=u+1; long long o;
I int max(int x,int y){return x>y?x:y;}
I int min(int x,int y){return x<y?x:y;}
I void add(int x,int y){s[++c]=h[x],h[x]=c,g[c]=y;}
void dfs1(int x,int f){p[x]=f,d[x]=t[x]=d[f]+1;
for(R int i=h[x],y;i;i=s[i])
if((y=g[i])^f){dfs1(y,x);
if(t[y]>t[x]) t[x]=t[y],q[x]=y;
}
}
void dfs2(int x){f[x][0]=1;
R int i,j,y,m,n=t[x]-d[x],k;
if(q[x]) f[q[x]]=f[x]+1,dfs2(q[x]);
for(i=h[x];i;i=s[i])
if((y=g[i])^p[x]&&y^q[x]){f[y]=e,m=t[y]-d[y],e+=m+1,dfs2(y);
for(j=max(K-n,0),k=min(m,K-1);j<=k;++j) o+=f[x][K-j]*f[y][j];
for(j=0;j<=m;++j) f[x][j+1]+=f[y][j];
}
if(n>K) o+=f[x][K+1];
}
int main(){
R int n=rd(),i,x,y;
for(K=rd()-1,i=1;i<n;++i) x=rd(),y=rd(),add(x,y),add(y,x);
dfs1(1,0),f[1]=e,e+=t[1],dfs2(1),printf("%lld",o);
return 0;
}

CF161D Distance in Tree的更多相关文章

  1. CF161D Distance in Tree(点分治)

    点分治是一种处理树的优秀暴力 这是一道板子题 #include <cstdio> #include <cstring> #include <algorithm> u ...

  2. 洛谷P3806 点分治1 & POJ1741 Tree & CF161D Distance in Tree

    正解:点分治 解题报告: 传送门1! 传送门2! 传送门3! 点分治板子有点多,,,分开写题解的话就显得很空旷,不写又不太好毕竟初学还是要多写下题解便于理解 于是灵巧发挥压行选手习惯,开始压题解(bu ...

  3. CF161D Distance in Tree 点分治

    题目: 输入点数为N一棵树,求树上长度恰好为K的路径个数 分析: 题目的数据范围不是很紧,点分治也可以过,树形dp也可以过.这里采用点分治做法. 我们只需要单开一个类似于桶的数组,跑点分治套路,统计即 ...

  4. [CF161D]Distance in Tree-树状dp

    Problem Distance in tree 题目大意 给出一棵树,求这棵树上有多少个最短距离为k的点对. Solution 这个题目可以用点分治来做,然而我到现在还是没有学会点分治,所以只好用树 ...

  5. codeforces 161D Distance in Tree 树形dp

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

  6. 【树形dp】Distance in Tree

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

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

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

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

  9. Distance in Tree CodeForces - 161D

    Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...

随机推荐

  1. Guava包学习--Multiset

    Multiset之前倒是没用过,后来看了下还挺有用,其实它就是支持重复元素的HashSet,相当于list+set的集合,综合了两种集合的优点. 它扩展了Collection: @GwtCompati ...

  2. 1878. [SDOI2009]HH的项链【线段树 或 莫队】

    Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一 段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此他的项链变得 ...

  3. IE6/IE7不支持first-child的解决办法

    #sidebar li:first-child{ border-top-style:none; } #sidebar li{ border-top-width:1px; border-top-styl ...

  4. Microsoft visual c++ 14.0 is required问题

    错误信息: error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Too ...

  5. scrapy---setting的字段含义

    # -*- coding: utf-8 -*- # Scrapy settings for lizi project # # For simplicity, this file contains on ...

  6. maven加速镜像

    <mirror> <id>repo3</id> <mirrorOf>central</mirrorOf> <name>Human ...

  7. [USACO06NOV]玉米田$Corn \ \ Fields$ (状压$DP$)

    #\(\mathcal{\color{red}{Description}}\) \(Link\) 农场主\(John\)新买了一块长方形的新牧场,这块牧场被划分成\(M\)行\(N\)列\((1 ≤ ...

  8. ORACLE 中rownum和row_number()的使用区别(可指定取sql结果集的第几个数据)

    这篇文章主要介绍了oracle中rownum和row_number()的使用方法以及区别和联系,十分的详细,有需要的小伙伴可以参考下.   row_number()over(partition by ...

  9. 框架 Hibernate

    Hibernate 在test01右键新建其他找到hibernate文件夹下的Hibernate Configuration File(cfg.xml) <?xml version=" ...

  10. npm audit fix

    执行npm install 出现如下提醒   added 253 packages from 162 contributors and audited 1117 packages in 42.157s ...