Codeforces 23E (大数乘法+树形dp)
来源:互联网 发布:mastercam9.1四轴编程 编辑:程序博客网 时间:2024/05/29 05:01
把一颗树分成几个联通块后的大小乘积最大。
dp[i][j]表示第i个节点下面有j-1个儿子的乘积最大值。
//树形dp一般从最上面递归到最下面,然后往上转移。
import java.util.*;import java.io.*;import java.math.*;public class Main{ static BigInteger dp[][]=new BigInteger[705][705];//表示第i个节点下面有j-1个儿子的乘积最大值 static int to[]=new int[1410]; static int nxt[]=new int[1410]; static int head[]=new int[705],cnt,n; static void add(int u,int v) { to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; } static int siz[] = new int[705]; static void dfs(int u,int pre) { int v; siz[u]=1; for(int i=0;i<=n;i++) dp[u][i]=BigInteger.ONE; for(int i=head[u];i!=0;i=nxt[i]) { v=to[i]; if(v==pre) continue; dfs(v,u);//往下找到最低的儿子,从最低的儿子开始更新上去 for(int j=siz[u];j>=0;j--) for(int k=siz[v];k>=0;k--) dp[u][j+k]=dp[u][j+k].max(dp[u][j].multiply(dp[v][k])); siz[u]+=siz[v]; } for(int i=1;i<=siz[u];i++) dp[u][0]=dp[u][0].max(dp[u][i].multiply(BigInteger.valueOf(i))); } public static void main(String[]args) throws IOException { Scanner in = new Scanner(System.in); n = in.nextInt(); cnt = 0; for (int i = 1; i < n; i++) { int u = in.nextInt(), v = in.nextInt(); add(u, v); add(v, u); } dfs(1, -1); System.out.println(dp[1][0]); }}
0 0
- Codeforces 23E (大数乘法+树形dp)
- codeforces 23E 树形DP
- codeforces 23E 树形DP
- codeforces 23E Tree (树形dp)
- Codeforces 23 E Tree 树形dp+高精
- codeforces 538E E. Demiurges Play Again(博弈+树形dp)
- DP训练 Codeforces 816E Karen And SuperMarket [树形DP]
- 【树形DP】 CodeForces 294E Shaass the Great
- CodeForces 158E Phone Talks 树形dp+计数
- codeforces 294E Shaass the Great (树形dp,好题)
- Codeforces 294E Shaass the Great 树形dp(水
- Codeforces 212E IT Restaurants 树形dp(水
- codeforces 600E. Lomsat gelral(教育场 树形dp)
- Educational Codeforces Round 2 E.Lomsat gelral(树形dp)
- CodeForces 600E Lomsat gelral(树形dp+启发式合并)
- Codeforces Round #397 E. Tree Folding (树形dp)
- Codeforces 538E. Demiurges Play Again (树形DP)
- Codeforces 239E World Eater Brothers【思维+树形Dp】
- Centos7安装redis3.0
- Ubuntu添加用户并赋sudo权限
- git 错误:
- strcpy函数的实现
- Objective-C Category 的实现原理
- Codeforces 23E (大数乘法+树形dp)
- linux下使用Drcom登校园网方法汇总
- 01-nginx下载与安装
- 最全前端资源汇集
- Android onMeasure,onLayout,onDraw的理解
- ISO Country Code - 标准国家码
- MySQL入门学习时,所遇到的问题(1)
- android自定义view之汽车仪表盘增强版
- 快速求解一个数位数的和