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. HBase技术介绍

    HBase简介 HBase - Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. HB ...

  2. webservice2-wsimport的使用

    打开Eclipse,新建java项目,service_start cmd下运行wsimport(要配置jdk的环境变量,wsimport在JAVA_HOME/bin下) wsimport -d d:/ ...

  3. 关于xcode7编译旧项目崩溃-[UIApplication _runWithMainScene:transitionContext:completion:]

    崩溃原因 crash: Assertion failure in -[UIApplication _runWithMainScene:transitionContext:completion:], / ...

  4. 让Barebox正确引导Tiny6410的linux内核

    本文专属tiny6410吐槽系列~如有得罪,请多包涵~在此也留个联系方式~291222536@qq.com,欢迎大家一起交流吐槽心得~ 上节各位搞得如何~这次怎么修改barebox的一些小细节,并且配 ...

  5. 【mac开发.NET】No installed provisioning profiles match the installed iOS signing identities

    编译错误提示 /Library/Frameworks/Mono.framework/External/xbuild/Xamarin/iOS/Xamarin.iOS.Common.targets: Er ...

  6. 百度——LBS.云 v2.0——创建自己的地理云数据

    随着云技术和地理信息(GIS)技术的发展,今年终于进入了.地理分享的新纪元.百度提供了LBS的云存储.真是个不错的功能.下面让我们来看看如何使用吧. 1.注册百度开发者账号(此处略去88个字) 2.创 ...

  7. 深入理解ob_flush和flush的区别(转)

    ob_flush/flush在手册中的描述, 都是刷新输出缓冲区, 并且还需要配套使用, 所以会导致很多人迷惑… 其实, 他们俩的操作对象不同, 有些情况下, flush根本不做什么事情.. ob_* ...

  8. Garbage Collectors - Serial vs. Parallel vs. CMS vs. G1 (and what's new in Java 8)--转

    The 4 Java Garbage Collectors - How the Wrong Choice Dramatically Impacts Performance The year is 20 ...

  9. linux telnet服务安装与配置

    关闭防火墙:service iptabls stop            chkconfig iptabls off 1.安装telnet服务 [root@rheltest1 ~]# rpm -qa ...

  10. 【转】非常详细的docker学习笔记

    一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化平台 Docker Hub: 用于分享.管理 Docker 容器的 Docker SaaS 平台 -- Docke ...