Codeforces 23E Tree
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的更多相关文章
- Codeforces 23E Tree(树型DP)
题目链接 Tree $dp[x][i]$表示以x为根的子树中x所属的连通快大小为i的时候 答案最大值 用$dp[x][j]$ * $dp[y][k]$ 来更新$dp[x][j + k]$. (听高手说 ...
- Codeforces 675D Tree Construction Splay伸展树
链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组 异或
http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...
- Codeforces 570D - Tree Requests【树形转线性,前缀和】
http://codeforces.com/contest/570/problem/D 给一棵有根树(50w个点)(指定根是1号节点),每个点上有一个小写字母,然后有最多50w个询问,每个询问给出x和 ...
- Codeforces 1092F Tree with Maximum Cost(树形DP)
题目链接:Tree with Maximum Cost 题意:给定一棵树,树上每个顶点都有属性值ai,树的边权为1,求$\sum\limits_{i = 1}^{n} dist(i, v) \cdot ...
- [Educational Round 17][Codeforces 762F. Tree nesting]
题目连接:678F - Lena and Queries 题目大意:给出两个树\(S,T\),问\(S\)中有多少连通子图与\(T\)同构.\(|S|\leq 1000,|T|\leq 12\) 题解 ...
- Codeforces 911F Tree Destruction
Tree Destruction 先把直径扣出来, 然后每个点都和直径的其中一端组合, 这样可以保证是最优的. #include<bits/stdc++.h> #define LL lon ...
- CodeForces 570D - Tree Requests - [DFS序+二分]
题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到DFS序. 然后,我们如果再把所有节点分层存下来,那么显然可以根 ...
- 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 ...
随机推荐
- G - Balanced Lineup - poj3264(区间查询)
题意:给你一组值,然后询问某个区间的最大值和最小值得差 分析:因为没有更新,所以只需要查找即可,节点保存一个最大值最小值就行了 ************************************ ...
- C# 关键字--using
using 关键字有两个主要用途: (一).作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型. (二).作为语句,用于定义一个范围,在此范围的末尾将释放对象. using指令 ...
- motan源码分析八:涉及到底层的客户端调用
之前我们分析了客户端调用服务端的源码,但是没有涉及到通讯层和序列化层,本文将之前讲过的内容做一次串联. 1.上层通过动态代理调用refer的call,每个refer又对应一个nettyclient,下 ...
- BitBlt介绍
设备上下文画图有非常多种方法.比如通过创建位图画刷,利用其填充一个区域来实现图像的绘制.此外,还能够使用CDC类的位图函数来输出位图到设备上下文中. BitBlt 用于从原设备中复制位图到目标设备,语 ...
- soundPool和audiofocus
audiofocus试验: 使用soundPool来写一个播放音频的porject. 资源初始化: setContentView(R.layout.activity_main); Button bt1 ...
- AndroidStudio文件夹结构视图讲解
近期,Google已经打算废弃Eclipse.而要大力支持他的亲生儿子AndroidStudio了,已经不在维护Eclipse了,也就是说在Eclipse上面出了什么问题.Google已经不在会管了, ...
- Android四大组件——Activity
Activity作为Android四大组件之一,也是其中最重要的一个组件.作为一个与用户交互的组件,我们可以把Activity比较成为windows系统上的一个文件夹窗口,是一个与用户交互的界面.再进 ...
- XgCalendar日历插件动态添加参数
在使用xgcalendar日历插件的时候,参数数组并非只有类型.显示时间.时区等这些参数,还可以根据extParam自定义参数扩展搜索条件,例如根据用户Id搜索不同用户的日历信息,需要将用户的Id存在 ...
- 95秀-dialog 进度对话框 实用工具
工具Util public class DialogUtil { public static ProgressDialogView progressDialog; /** * ...
- C# Gma.QrCodeNet生成二维码
一.使用C#生成二维码类库之一是qrcodenet 源代码地址: http://qrcodenet.codeplex.com/ 二.使用实例 定义处理字符串 static string url = & ...