题目链接

  我貌似又做了一道高精题呢(笑)

  这题的DP方程很好想,设f[i][j]表示i为根的子树,i所在联通块大小为j的最大值,然后乱搞

  但是要高精,那么搞是得要高精除的

  所以考虑f[i][j]是除以j后的最大值,就可以只写高精乘了

  不过卡常,下面代码只能得95分

// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<cstdio>
#include<iostream>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define maxl 120
#define maxn 702
#define check(x) if(x==0) x=++tot;
using namespace std; inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} struct BigInteger{
int s[maxl],len;
BigInteger(){len=;memset(s,,sizeof(s));}
BigInteger operator =(const char *c){
len=;
for(int i=strlen(c+);i;--i) s[++len]=c[i]-'';
return *this;
}
BigInteger operator =(const int num){
char c[maxl];
sprintf(c+,"%d",num);
(*this)=c;
return *this;
}
BigInteger operator *(BigInteger a){
BigInteger ans;
ans.len=len+a.len;
for(int i=;i<=len;++i)
for(register int j=;j<=a.len;++j)
ans.s[i+j-]+=s[i]*a.s[j];
int g=;
for(int i=;i<=(len+a.len)||g;++i){
ans.s[i]+=g;
g=ans.s[i]/;
ans.s[i]%=;
if(ans.len<i) ans.len=i;
}
while(ans.len&&ans.s[ans.len]==) ans.len--;
return ans;
}
BigInteger operator *(const int a){
BigInteger ans=(*this);
int g=,lim=ans.len;
for(int i=;(i<=lim)||g;++i){
ans.s[i]=ans.s[i]*a+g;
g=ans.s[i]/;
ans.s[i]%=;
if(i>ans.len) ans.len=i;
}
while(ans.len&&ans.s[ans.len]==) ans.len--;
return ans;
}
bool operator <(const BigInteger a)const{
if(len!=a.len) return len<a.len;
for(int i=len;i;--i)
if(s[i]!=a.s[i]) return s[i]<a.s[i];
return ;
}
}; inline void print(BigInteger a){
if(a.len==) putchar('');
for(int i=a.len;i;--i) putchar(a.s[i]+'');
} BigInteger f[maxn*];
int tot;
int size[maxn];
int pos[maxn][maxn];
BigInteger ans; struct Edge{
int next,to;
}edge[maxn*];
int head[maxn],num;
inline void add(int from,int to){
edge[++num]=(Edge){head[from],to};
head[from]=num;
} void dfs(int x,int fa){
check(pos[x][]); check(pos[x][]);
f[pos[x][]]=;f[pos[x][]]=; size[x]=;
for(int i=head[x];i;i=edge[i].next){
int to=edge[i].to;
if(to==fa) continue;
dfs(to,x);
size[x]+=size[to];
for(int j=size[x];j>=;--j){
check(pos[x][j]);
for(int k=min(j,size[x]-size[to]);k>=min(,size[x]-size[to]);--k)
if(f[pos[x][j]]<f[pos[to][j-k]]*f[pos[x][k]]){
f[pos[x][j]]=f[pos[to][j-k]]*f[pos[x][k]];
}
}
}
for(int j=;j<=size[x];++j){
if(f[pos[x][]]<f[pos[x][j]]*j){
f[pos[x][]]=f[pos[x][j]]*j;
if(ans<f[pos[x][]]) ans=f[pos[x][]];
}
}
} int main(){
int n=read();
for(int i=;i<n;++i){
int x=read(),y=read();
add(x,y);
add(y,x);
}
dfs(,);
print(ans);
}

【Luogu】P1411树(树形高精DP)的更多相关文章

  1. 洛谷 P1411 树 (树形dp)

    大意: 给定树, 求删除一些边, 使得连通块大小的乘积最大 设$dp_{i,j}$表示只考虑点$i$的子树, $i$所在连通块大小为$j$的最大值. 转移的时候不计算$i$所在连通块的贡献, 留到最后 ...

  2. 与高精死杠的几天——记两道简单的高精dp

    (同样也是noip往年的题 1​.矩阵取数游戏 题目链接[Luogu P1005 矩阵取数游戏] \(\mathcal{SOLUTION}:\) 通过对题目条件的分析,我们可以发现,每一行取数对答案的 ...

  3. [CEOI2007]树的匹配Treasury(树形DP+高精)

    题意 给一棵树,你可以匹配有边相连的两个点,问你这棵树的最大匹配时多少,并且计算出有多少种最大匹配. N≤1000,其中40%的数据答案不超过 108 题解 显然的树形DP+高精. 这题是作为考试题考 ...

  4. BZOJ1089 [SCOI2003]严格n元树 【dp + 高精】

    Description 如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树.如果该树中最底层的节点深度为d (根的深度为0),那么我们称它为一棵深度为d的严格n元树.例如,深度为2的严 ...

  5. bzoj 1089: [SCOI2003]严格n元树【dp+高精】

    设f[i]为深度为i的n元树数目,s为f的前缀和 s[i]=s[i-1]^n+1,就是增加一个根,然后在下面挂n个子树,每个子树都有s[i-1]种 写个高精就行了,好久没写WA了好几次-- #incl ...

  6. 【BZOJ-3572】世界树 虚树 + 树形DP

    3572: [Hnoi2014]世界树 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1084  Solved: 611[Submit][Status ...

  7. 【BZOJ-2286】消耗战 虚树 + 树形DP

    2286: [Sdoi2011消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2120  Solved: 752[Submit][Status] ...

  8. BZOJ.1210.[HNOI2004]邮递员(插头DP Hash 高精)

    BZOJ 洛谷 http://www.cnblogs.com/LadyLex/p/7326874.html 插头DP.\(m+1\)个插头的状态需要用三进制表示:\(0\)表示无插头,\(1\)表示是 ...

  9. [P1005][NOIP2007] 矩阵取数游戏 (DP+高精)

    我不会高精…… 也不会DP…… 这道题即考高精又考DP…… 我要死了 给一个不是高精的代码(当然不能满分) #include<cstdio> #include<iostream> ...

随机推荐

  1. Installing Apache, PHP, and MySQL on Mac OS X

    I have installed Apache, PHP, and MySQL on Mac OS X since Leopard. Each time doing so by hand. Each ...

  2. python基础教程总结12——数据库

    1. Python 数据库 API 很多支持SQL标准的数据库在Python中都有对应的客户端模块.为了在提供相同功能(基本相同)的不同模块之间进行切换(兼容),Python 规定了一个标准的 DB ...

  3. cv2.solvepnp 相机的位姿估计

    预备知识   图像坐标系:   理想的图像坐标系原点O1和真实的O0有一定的偏差,由此我们建立了等式(1)和(2),可以用矩阵形式(3)表示. 相机坐标系(C)和世界坐标系(W): 通过相机与图像的投 ...

  4. 点击按钮在表格的某一行下,在添加一行(HTML+JS)

    使用js在指定的tr下添加一个新的一行newTr html代码: <table> <tr> <td>用户名:</td> <td><in ...

  5. MySQL 5.7.20绿色版安装详细图文教程

    MySQL 5.7.20绿色版安装详细图文教程 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle旗下产品.这篇文章主要介绍了MySQL 5.7.20绿色版安装 ...

  6. 12_1_Annotation注解

    12_1_Annotation注解 1. 什么是注解 Annotation是从JDK5.0开始引入的新技术. Annotation的作用: 不是程序本身,可以对程序作出解释.可以被其他程序(比如,编译 ...

  7. (转发)InputAccessoryView的使用方法

    转自:http://blog.sina.com.cn/s/blog_45e2b66c01015we9.html UITextFields and UITextViews have an inputAc ...

  8. OC和C++的混用2

    苹果的Objective-C编译器允许用户在同一个源文件里自由地混合使用C++和Objective-C,混编后的语言叫Objective-C++.有了它,你就可以在Objective-C应用程序中使用 ...

  9. 更改BootStrap popover的默认样式

    .popover { position: absolute; top: 0; left: 0; z-index: 1060; display: none; max-width: 276px; padd ...

  10. 洛谷 P3601 签到题

    https://www.luogu.org/problemnew/show/P3601 一道关于欧拉函数的题. 读完题目以后我们知道所谓的$aindao(x)=x- \phi (x) $. 对于x小的 ...