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

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

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

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define wk sb
#define ll long long
int tot,son[],first[],next[],go[];
int n;
struct node{
int a[],n;
}f[][];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
}
void add(int x,int y){
insert(x,y);insert(y,x);
}
node operator *(node a,node b){
node c;c.n=;for (int i=;i<=;i++) c.a[i]=;
c.n=a.n+b.n;
for (int i=;i<=a.n;i++)
for (int j=;j<=b.n;j++){
c.a[i+j-]+=a.a[i]*b.a[j];
c.a[i+j]+=c.a[i+j-]/;
c.a[i+j-]%=;
}
int j=;
while (j<=c.n||c.a[j]>){
c.a[j+]+=c.a[j]/;
c.a[j]%=;
j++;
}
while (j>&&c.a[j]==) j--;
c.n=j;
return c;
}
node make(int x){
node c;
c.n=;for (int i=;i<=;i++) c.a[i]=;
while (x){
c.a[++c.n]=x%;
x/=;
}
return c;
}
node up(node a,node b){
if (a.n>b.n) return a;
if (b.n>a.n) return b;
for (int i=a.n;i>=;i--)
if (a.a[i]>b.a[i]) return a;
else
if (b.a[i]>a.a[i]) return b;
return a;
}
void dfs(int x,int fa){
f[x][]=make();
son[x]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
dfs(pur,x);
for (int j=son[x];j>=;j--)
for (int k=son[pur];k>=;k--)
f[x][j+k]=up(f[x][j+k],f[x][j]*f[pur][k]);
son[x]+=son[pur];
}
f[x][]=make();
for (int i=;i<=son[x];i++)
f[x][]=up(f[x][],f[x][i]*make(i));
}
int main(){
n=read();
for (int i=;i<n;i++){
int x=read(),y=read();
add(x,y);
}
dfs(,);
printf("%d",(int)f[][].a[f[][].n]);
for (int i=f[][].n-;i>=;i--)
printf("%.4d",(int)f[][].a[i]);
}

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. adb Logcat用法

    转自: http://blog.csdn.net/tiantianshangcha/article/details/6288537 个人认为有一下几个常用命令: adb logcat -b radio ...

  2. 重写扫雷(基于jQuery) 新手 有不足的地方敬请谅解

    记得刚开始学习js的时候写过一次扫雷,一个下午的时间被计算搞死,整个头是晕乎. 入职后,蹭着空闲的时间随手写了一个扫雷. 直接上代码了 (function() { function module() ...

  3. MySQL中char、varchar和text的区别

    三者空间占用方面: char:存储定长数据很方便,CHAR字段上的索引效率极高,可以有默认值,比如定义char(10),那么不论你存储的数据是否达到了10个字节,都要占去10个字节的空间(自动用空格填 ...

  4. SKTexture类

    继承自 NSObject 符合 NSCodingNSCopyingNSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit.framewo ...

  5. Android Toast 自定义

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  6. XML自己定义检查器语法+约束(1)

    每次使用它检查xml文件时,仅仅需改动xmldoc.load("xml文件名称");中的文件名称,然后将该文件放在浏览器中执行就可以. 依据浏览器弹出的对话框进行推断自己写的xml ...

  7. SQL给查询结果加序号

    情境:在用delphi7编程时,想要给查询出的结果一个编号,比方有一万条结果,就自己主动从1编号到10000 显示数据时用的是DBGrid控件,可是它的第一列无法非常好的显示编号,找了非常多方法都不能 ...

  8. Android中的多媒体显示之图片缩放

    一:图片OOM异常: 代码示例: public class MainActivity extends Activity { private ImageView iv_imageView; protec ...

  9. 一个Nodejs的简单计算測试程序

    測试目的: 1 測试二维数组的使用 2 输出函数的使用 代码: var util = require('util'); a = 3; b = 4; c = a + b; a = []; for(i = ...

  10. Cookie Version in J2EE

    Cookie Version in J2EE 原文章:http://villadora.me/2014/05/06/cookie-version/ 在处理Cookie的时候发现不能处理servlet ...