http://codeforces.com/problemset/problem/23/E

题意:给一个树,求砍断某些边,使得所有联通块大小的乘积最大。
思路:f[i][j]代表当前把j个贡献给i的父亲(也就是不计入f[i][j])的最大乘积是多少,转移就是背包转移

记得最后统计答案的时候是f[i][j]*j

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<iostream>
  6. #define wk sb
  7. #define ll long long
  8. int tot,son[],first[],next[],go[];
  9. int n;
  10. struct node{
  11. int a[],n;
  12. }f[][];
  13. int read(){
  14. int t=,f=;char ch=getchar();
  15. while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
  16. while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
  17. return t*f;
  18. }
  19. void insert(int x,int y){
  20. tot++;
  21. go[tot]=y;
  22. next[tot]=first[x];
  23. first[x]=tot;
  24. }
  25. void add(int x,int y){
  26. insert(x,y);insert(y,x);
  27. }
  28. node operator *(node a,node b){
  29. node c;c.n=;for (int i=;i<=;i++) c.a[i]=;
  30. c.n=a.n+b.n;
  31. for (int i=;i<=a.n;i++)
  32. for (int j=;j<=b.n;j++){
  33. c.a[i+j-]+=a.a[i]*b.a[j];
  34. c.a[i+j]+=c.a[i+j-]/;
  35. c.a[i+j-]%=;
  36. }
  37. int j=;
  38. while (j<=c.n||c.a[j]>){
  39. c.a[j+]+=c.a[j]/;
  40. c.a[j]%=;
  41. j++;
  42. }
  43. while (j>&&c.a[j]==) j--;
  44. c.n=j;
  45. return c;
  46. }
  47. node make(int x){
  48. node c;
  49. c.n=;for (int i=;i<=;i++) c.a[i]=;
  50. while (x){
  51. c.a[++c.n]=x%;
  52. x/=;
  53. }
  54. return c;
  55. }
  56. node up(node a,node b){
  57. if (a.n>b.n) return a;
  58. if (b.n>a.n) return b;
  59. for (int i=a.n;i>=;i--)
  60. if (a.a[i]>b.a[i]) return a;
  61. else
  62. if (b.a[i]>a.a[i]) return b;
  63. return a;
  64. }
  65. void dfs(int x,int fa){
  66. f[x][]=make();
  67. son[x]=;
  68. for (int i=first[x];i;i=next[i]){
  69. int pur=go[i];
  70. if (pur==fa) continue;
  71. dfs(pur,x);
  72. for (int j=son[x];j>=;j--)
  73. for (int k=son[pur];k>=;k--)
  74. f[x][j+k]=up(f[x][j+k],f[x][j]*f[pur][k]);
  75. son[x]+=son[pur];
  76. }
  77. f[x][]=make();
  78. for (int i=;i<=son[x];i++)
  79. f[x][]=up(f[x][],f[x][i]*make(i));
  80. }
  81. int main(){
  82. n=read();
  83. for (int i=;i<n;i++){
  84. int x=read(),y=read();
  85. add(x,y);
  86. }
  87. dfs(,);
  88. printf("%d",(int)f[][].a[f[][].n]);
  89. for (int i=f[][].n-;i>=;i--)
  90. printf("%.4d",(int)f[][].a[i]);
  91. }

Codeforces 23E Tree的更多相关文章

  1. Codeforces 23E Tree(树型DP)

    题目链接 Tree $dp[x][i]$表示以x为根的子树中x所属的连通快大小为i的时候 答案最大值 用$dp[x][j]$ * $dp[y][k]$ 来更新$dp[x][j + k]$. (听高手说 ...

  2. Codeforces 675D Tree Construction Splay伸展树

    链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...

  3. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  4. Codeforces 570D - Tree Requests【树形转线性,前缀和】

    http://codeforces.com/contest/570/problem/D 给一棵有根树(50w个点)(指定根是1号节点),每个点上有一个小写字母,然后有最多50w个询问,每个询问给出x和 ...

  5. Codeforces 1092F Tree with Maximum Cost(树形DP)

    题目链接:Tree with Maximum Cost 题意:给定一棵树,树上每个顶点都有属性值ai,树的边权为1,求$\sum\limits_{i = 1}^{n} dist(i, v) \cdot ...

  6. [Educational Round 17][Codeforces 762F. Tree nesting]

    题目连接:678F - Lena and Queries 题目大意:给出两个树\(S,T\),问\(S\)中有多少连通子图与\(T\)同构.\(|S|\leq 1000,|T|\leq 12\) 题解 ...

  7. Codeforces 911F Tree Destruction

    Tree Destruction 先把直径扣出来, 然后每个点都和直径的其中一端组合, 这样可以保证是最优的. #include<bits/stdc++.h> #define LL lon ...

  8. CodeForces 570D - Tree Requests - [DFS序+二分]

    题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到DFS序. 然后,我们如果再把所有节点分层存下来,那么显然可以根 ...

  9. Codeforces 932D - Tree

    932D - Tree 思路: 树上倍增 anc[i][u]:u的2^i祖先 mx[i][u]:u到它的2^i祖先之间的最大值,不包括u pre[i][u]:以u开始的递增序列的2^i祖先 sum[i ...

随机推荐

  1. G - Balanced Lineup - poj3264(区间查询)

    题意:给你一组值,然后询问某个区间的最大值和最小值得差 分析:因为没有更新,所以只需要查找即可,节点保存一个最大值最小值就行了 ************************************ ...

  2. C# 关键字--using

    using 关键字有两个主要用途:  (一).作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型.  (二).作为语句,用于定义一个范围,在此范围的末尾将释放对象. using指令    ...

  3. motan源码分析八:涉及到底层的客户端调用

    之前我们分析了客户端调用服务端的源码,但是没有涉及到通讯层和序列化层,本文将之前讲过的内容做一次串联. 1.上层通过动态代理调用refer的call,每个refer又对应一个nettyclient,下 ...

  4. BitBlt介绍

    设备上下文画图有非常多种方法.比如通过创建位图画刷,利用其填充一个区域来实现图像的绘制.此外,还能够使用CDC类的位图函数来输出位图到设备上下文中. BitBlt 用于从原设备中复制位图到目标设备,语 ...

  5. soundPool和audiofocus

    audiofocus试验: 使用soundPool来写一个播放音频的porject. 资源初始化: setContentView(R.layout.activity_main); Button bt1 ...

  6. AndroidStudio文件夹结构视图讲解

    近期,Google已经打算废弃Eclipse.而要大力支持他的亲生儿子AndroidStudio了,已经不在维护Eclipse了,也就是说在Eclipse上面出了什么问题.Google已经不在会管了, ...

  7. Android四大组件——Activity

    Activity作为Android四大组件之一,也是其中最重要的一个组件.作为一个与用户交互的组件,我们可以把Activity比较成为windows系统上的一个文件夹窗口,是一个与用户交互的界面.再进 ...

  8. XgCalendar日历插件动态添加参数

    在使用xgcalendar日历插件的时候,参数数组并非只有类型.显示时间.时区等这些参数,还可以根据extParam自定义参数扩展搜索条件,例如根据用户Id搜索不同用户的日历信息,需要将用户的Id存在 ...

  9. 95秀-dialog 进度对话框 实用工具

    工具Util public class DialogUtil {     public static ProgressDialogView progressDialog;     /**      * ...

  10. C# Gma.QrCodeNet生成二维码

    一.使用C#生成二维码类库之一是qrcodenet 源代码地址: http://qrcodenet.codeplex.com/ 二.使用实例 定义处理字符串 static string url = & ...