CodeForces 160D - Distance in Tree 树型DP
题目给了512MB的空间....用dp[k][i]代表以k为起点...往下面走(走直的不打岔)i步能有多少方案....在更新dp[k][i]过程中同时统计答案..
Program:
#include<iostream>
#include<queue>
#include<stack>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#define ll long long
#define oo 1000000007
#define MAXN 50005
using namespace std;
struct node
{
int x,y,next;
}line[MAXN*2];
int n,K,dp[MAXN][502],_next[MAXN];
bool used[MAXN];
ll ans;
void addline(int x,int y,int m)
{
line[m].next=_next[x],_next[x]=m;
line[m].x=x,line[m].y=y;
return;
}
void dfs(int x)
{
int i,j,k;
k=_next[x];
dp[x][0]=1;
while (k)
{
if (!used[line[k].y])
{
used[line[k].y]=true;
dfs(line[k].y);
for (i=K;i>=1;i--) ans+=dp[x][K-i]*dp[line[k].y][i-1];
for (i=K;i>=1;i--) dp[x][i]+=dp[line[k].y][i-1];
}
k=line[k].next;
}
return;
}
int main()
{
int i,j;
while (~scanf("%d%d",&n,&K))
{
memset(_next,0,sizeof(_next));
for (i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
addline(x,y,i*2-1);
addline(y,x,i*2);
}
memset(used,false,sizeof(used));
memset(dp,0,sizeof(dp));
used[1]=true;
ans=0;
dfs(1);
printf("%I64d\n",ans);
}
return 0;
}
CodeForces 160D - Distance in Tree 树型DP的更多相关文章
- Codeforces 161D Distance in Tree(树型DP)
题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...
- D. Distance in Tree(树型Dp计数)
\(其实思路都能想到一点,就是去重这里特别麻烦,没有好的思路.\) \(设dp[i][j]为以i为根深度为j的节点数量\) \(dp[parent][j]=\sum{dp[son][j-1]}\) \ ...
- Codeforces 149D Coloring Brackets(树型DP)
题目链接 Coloring Brackets 考虑树型DP.(我参考了Q巨的代码还是略不理解……) 首先在序列的最外面加一对括号.预处理出DFS树. 每个点有9中状态.假设0位不涂色,1为涂红色,2为 ...
- Educational Codeforces Round 52 (Rated for Div. 2) F. Up and Down the Tree 树型DP
题面 题意:给你一棵树,你起点在1,1也是根节点,你每次可以选择去你子树的某个叶子节点,也可以选择,从叶子节点返回距离不超过k的一个根, 也就是说,你从1开始,向下跳,选择一个叶子(就是没有子树的节点 ...
- POJ 2486 Apple Tree ( 树型DP )
#include <iostream> #include <cstring> #include <deque> using namespace std; #defi ...
- CodeForces 161D Distance in Tree【树形DP】
<题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include &l ...
- Codeforces 461B - Appleman and Tree 树状DP
一棵树上有K个黑色节点,剩余节点都为白色,将其划分成K个子树,使得每棵树上都仅仅有1个黑色节点,共同拥有多少种划分方案. 个人感觉这题比較难. 如果dp(i,0..1)代表的是以i为根节点的子树种有0 ...
- Codeforces 486D Valid Sets (树型DP)
题目链接 Valid Sets 题目要求我们在一棵树上计符合条件的连通块的个数. 满足该连通块内,点的权值极差小于等于d 树的点数满足 n <= 2000 首先我们先不管这个限制条件,也就是先考 ...
- HDU 5905 Black White Tree(树型DP)
题目链接 Black White Tree 树型DP,设$f[i][j]$为以$i$为根的子树中大小为$j$的连通块中可以包含的最小黑点数目. $g[i][j]$为以$i$为根的子树中大小为$j$的 ...
随机推荐
- python递归函数下不能正常使用yield
# -*- coding:utf-8 -*- import os import time file_list = [] def findFile(path): listFile = os.listdi ...
- C语言如何定义结构体
原文地址 1. struct与typedef struct区别 struct是结构体的关键字,用来声明结构体变量如 struct student { char num[10]; ch ...
- Android NOTE
一些小的点就记在这里吧…… MultiDex打包时zip错误 我遇到的是 Execution failed for task ':excelSior:packageAllDebugClassesFor ...
- lint使用简介
LINT工具是一种软件质量保证工具,许多国外的大型专业软件公司,如微软公司,都把它作为程序检查工具,在程序合入正试版本或交付测试之前一定要保证通过了LINT检查,他们要求软件工程师在使用LINT时要打 ...
- Delphi中methodaddress的汇编代码解析
class function TObject.MethodAddress(const Name: ShortString): Pointer;asm { -> EAX ...
- 各类形参(引用,const,指针)
#include <stdlib.h> #include <iostream> //这是一个关于引用形参,const形参,指针形参的程序,用于理解不同形式的区别 using n ...
- 推荐两个不错的CAD二次开发(.Net)手册
推荐两个不错的CAD二次开发(.Net)手册 http://www.mjtd.com/helpcenter/netguide/index.html http://www.ceesky.com/book ...
- 使用sqlplus批量执行脚本的总结
当然,我们可以在plsql中执行,但是在实际生产环境中,可能更多的是使用简便的sqlplus.步骤如下: 1.登陆client sqlplus connect <username>/< ...
- inline函数和一般的函数有什么不同
1.比如: int g(int x) { return x + x; } int f() { return g(); } 这样f会调用g,然后g返回x + x给f,然后f继续把那个值返回给调用者. 如 ...
- 2014.8.18for循环
for循环 1.初始状态 2.循环条件 3.循环体 4.状态改变 语法 for( 初始状态 ; 循环条件 ; 状态改变 ) { 循环体; } eg: ; i <= ; i++) { Con ...