【树形DP】 codeforces VK Cup 2012 Round 1 D
来源:互联网 发布:ios 矩阵运算 编辑:程序博客网 时间:2024/04/28 04:34
原题直通车: codeforces VK Cup 2012 Round 1 D
题意: 一颗树有n个结点,边长度都为1,问树中距离为K的点有多少对.
分析:
对于某一结点A,到它距离为k的点有两种情况:
1)、在以A为根结点的子树中;
2)、在以兄弟结点为子结点的子树中(k>=2)如图(通过父结点与兄弟结点贯通)。
第一种用DFS可直接求出;
第二种:
如对于结点i,其父结点为fa[i].
假设i这个子树中距i等于j的点有x个,fa[i]子树中距fa[i]为k-j-1的结点有y 个,
y个当中又在i子树中的有z个, 那么x与(y-z)这些结点的距离为k,数量为x*(y-z)对。
PS:求第二种时,可能会算出重复的(正好是两倍)。
比如:对于图中k=3时。
⑴ 、对于结点i=2,j=0则x=1,(y-z)=2(结点6和8)。
⑵ 、对于结点i=3, j=1则x=2,(y-z)=1(结点2)。
上面两种情况其实只能算一种。所以最终结果得除以2。
#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn=55555;vector<int>Tree[maxn];int dp[maxn][555]; int pre[maxn];int n,k;void DFS(int cnt,int fa){ pre[cnt]=fa; int len=Tree[cnt].size(); for(int i=0;i<len;++i){ int son=Tree[cnt][i]; if(son==fa) continue; DFS(son,cnt); } dp[cnt][0]=1; if(fa) for(int i=0;i<k;++i) dp[fa][i+1]+=dp[cnt][i];}int main(){ cin>>n>>k; for(int i=1;i<n;++i){ int a,b; cin>>a>>b; Tree[a].push_back(b); Tree[b].push_back(a); } if(k==1){ cout<<n-1<<endl; return 0; } DFS(1,0); long long ans=0; for(int i=1;i<=n;++i){ if(!pre[i]) continue; ans+=(long long)dp[i][k-1]*2; for(int j=0;j<=k-2;++j) ans+=((long long)dp[i][j])*(dp[pre[i]][k-1-j]-dp[i][k-2-j]); } cout<<ans/2<<endl; return 0;}//
- 【树形DP】 codeforces VK Cup 2012 Round 1 D
- [树形DP]VK Cup 2012 Round 1 D. Distance in Tree
- 【树形dp统计距离为k的点对】VK Cup 2012 Round 1 D. Distance in Tree
- Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) D. Singer House(dp)
- 【dp+dfs】VK Cup 2012 Round 1-D. Distance in Tree
- VK Cup 2012 Round 1
- 【codeforces VK Cup Round 1】BDE题解
- 【dp】VK Cup 2012 Qualification Round 1 ——E
- VK Cup 2015 - Round 2 (unofficial online mirror, Div. 1 only) B. Work Group 树形dp
- VK Cup 2012 Qualification Round 1 E
- Codeforces VK Cup 2012 Qualification Round 1 / 158A Next Round(模拟)
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 1 Edition) C 离散化+树状数组+map D 数学
- VK Cup 2016 - Round 1 (Div. 2 Edition) C D
- Codeforces VK Cup 2015 - Round 1 (Div.1 A~E)
- Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) C. Bear and Company(DP)
- Codeforces VK Cup 2015 Wild Card Round 1 (AB)
- Codeforces VK Cup 2016 - Round 1 (Div. 2 Edition)
- VK Cup 2012 Round 2
- Android媒体扫描详细解析之一(MediaScanner & MediaProvider)
- MyEclipse提示Errors occurred during the build
- 设计模式和设计模式的六大原则
- StrutsPrepareAndExecuteFilter的作用
- OA的部门管理和员工管理模块的实现总结
- 【树形DP】 codeforces VK Cup 2012 Round 1 D
- Android Bitmap内存限制问题
- 可以查看java字节码的eclipse插件
- OA项目的审批流转模块实现
- 解决VC6.0资源窗口打不开的解决方法
- 求一个字符串中连续出现次数最多的子串
- hdu1874畅通工程续
- OA项目的表单管理模块
- 曲线scala