codeforces161D
题目大意
输入点数为N一棵树
求树上长度恰好为K的路径个数
输入格式
第一行两个数字N,K,如题意
接下来的N−1行中,每行两个整数u,v表示一条树边(u,v)
对于这种题我们一般都是直接遍历
因为直接遍历的桶通常比较小 比较优秀
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];
inline int read()
{
int date = 0,m = 1; char ch = 0;
while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
if(ch=='-'){m = -1; ch = getchar();}
while(ch>='0' && ch<='9')
{
date = date*10+ch-'0';
ch = getchar();
}return date*m;
}
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
/*int root[MAX_N],cnt,sz;
namespace hjt
{
#define mid ((l+r)>>1)
struct node{int l,r,maxx;}T[MAX_N*40];
#undef mid
}*/
const int MAX_N = 50025;
int n,k,p[MAX_N],eid,maxson[MAX_N],dep[MAX_N],root,ms,SIZE,Size[MAX_N],ans;
bool vis[MAX_N];
int num[505],tmp[505];
struct edge
{
int v,next,dis;
}e[MAX_N<<1];
void add(int u,int v,int dis)
{
e[eid].dis = dis;
e[eid].next = p[u];
e[eid].v = v;
p[u] = eid++;
}
void init()
{
memset(p,-1,sizeof(p));
eid = 0;
}
void getroot(int x,int fa)
{
Size[x] = 1;maxson[x] = 0;
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]||v==fa) continue;
getroot(v,x);
Size[x]+=Size[v];
if(Size[v]>maxson[x]) maxson[x] = Size[v];
}
if(SIZE-Size[x]>maxson[x]) maxson[x] = SIZE - Size[x];
if(ms>maxson[x]) ms = maxson[x],root = x;
}
void getdis(int x,int fa)
{
if(dep[x]>k) return;
ans+=num[k-dep[x]];++tmp[dep[x]];
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(v==fa||vis[v]) continue;
dep[v] = dep[x] + e[i].dis;
getdis(v,x);
}
}
void fenzhi(int x,int ssize)
{
vis[x] = true;
for(int i = 1;i<=k;++i) num[i] = 0;num[0] = 1;
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]) continue;
for(int j = 0;j<=k;++j) tmp[j] = 0;
dep[v] = 1;
getdis(v,x);
for(int j = 0;j<=k;++j) num[j] += tmp[j];
}
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]) continue;
ms = inf;root = 0;
SIZE = Size[v]<Size[x]?Size[v]:(ssize-Size[x]);
getroot(v,0);
fenzhi(root,(Size[v]<Size[x]?Size[v]:(ssize-Size[x])));
}
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int a,b;
scanf("%d%d",&n,&k);
init();
for(int i = 1;i<n;++i)
{
scanf("%d%d",&a,&b);
add(a,b,1);add(b,a,1);
}
ms = inf;root = 0;SIZE = n;
getroot(1,-1);
fenzhi(root,SIZE);
printf("%d\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}