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. [置顶] 软件架构师的12项修炼_读书纪要_P3商务技能修炼

    本系列的文章是我读书后的个人纪要(书摘),仅代表个人观点.如想了解更多相关内容,请购买正版物,支持原书作者.对应的图书可以从我的个人图书列表里找寻:个人毕业后图书列表 本书勾勒了软件架构师的必备技能的 ...

  2. C# 我是个传奇的 using

    呵呵呵: ----------------------------------------------------------------------------------------------- ...

  3. hibernate初涉

    好久都不曾写写总结一些东西了,惰性真的是令人难以克制!虽然和许多北漂族一样,艰苦而又迷茫,但是我总能找到一些方向,一点期盼,因为你就我的目标.我会坚持下去,重拾青春的热血,既然人生如戏,那我不当猪脚. ...

  4. jenkins服务器安装

    http://www.360doc.com/content/13/0412/09/10504424_277718090.shtml

  5. iOS键盘覆盖输入框的处理.doc

    在一个多项输入界面上,会有多个UITextfield类型的输入框.为了滚动方面,我们会将他们一一添加到UITableView的cell中,从而组成一个可以上下滑动的数据输入界面. 但是字符输入是通过系 ...

  6. Struts2框架具体解释

    在Struts2的Model-View-Controller模式实现下面五个核心组件: 动作-Actions 拦截器-Interceptors 值栈/OGNL 结果/结果类型 视图技术 Struts ...

  7. monkeyrunner 详细介绍

    MonkeyRunner: monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器.通过monkeyrunner,您可以写出一个 ...

  8. Unity3D Asset stored 已下载的位置

    Unity3D Asset stored下载资源在本地的什么目录里呢?C:\Users\accountName\AppData\Roaming\Unity\Asset Store

  9. 修改MySQL引擎

    1. 显示MySQL支持的引擎:show engines;

  10. 阿里云ECS安全改造:改用AWS EC2私钥方式登录

    阿里云ECS有多烂,用过AWS的人都知道,ECS有什么问题呢,简单总结一下: 1)ubuntu居然root直接访问 2)端口全开 3)WEB控制台无法定义安全组及关闭端口 4)云盾黑洞 的确用起来很糟 ...