E. Tree

状态表示:fu,jf_{u,j}fu,j​表示以uuu节点的子树,uuu所在连通块大小为jjj时,并且没有算上uuu连通块的贡献的最大值
状态计算:
对于一棵子树vvv来说,显然可以有两种情况

而答案就是max⁡[f1,1→sz1×(1→sz1)]\max[f_{1,1\to sz_{1}}×(1\to sz_1)]max[f1,1→sz1​​×(1→sz1​)](1是根节点,乘的这部分1→sz11\to sz_11→sz1​是该节点所在连通块的贡献)


你以为完了???不不不,还要写个高精度!!!

#define IO ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(0)
#pragma GCC optimize(2)
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
//==========================================================高精度板子
struct bign
{int d[120], len;bign() { memset(d, 0, sizeof d); len = 1; }bign(int num) { *this = num; }bign(char* num) { *this = num; }bign operator=(const char* num){len = strlen(num);for (int i = 0; i < len; i++) d[i] = num[len - i - 1] - '0';return *this;}bign operator=(int num){char c[1010];sprintf(c, "%d", num);*this = c;return *this;}void clear(){while (len > 1 && !d[len - 1]) len--;}bign operator+(const bign& b){bign c; c.len = 0;for (int i = 0, t = 0; t || i < len || i < b.len; i++){if (i < len) t += d[i];if (i < b.len) t += b.d[i];c.d[c.len++] = t % 10;t /= 10;}return c;}bign operator-(const bign& b){bign c; c.len = 0;for (int i = 0, t = 0; i < len; i++){t += d[i];if (i < b.len) t -= b.d[i];c.d[c.len++] = (t + 10) % 10;if (t >= 0) t = 0;else t = -1;}c.clear();return c;}bign operator*(const bign& b){bign c; c.len = len + b.len;for (int i = 0; i < len; i++) for (int j = 0; j < b.len; j++) c.d[i + j] += d[i] * b.d[j];for (int i = 0; i < c.len - 1; i++) c.d[i + 1] += c.d[i] / 10, c.d[i] %= 10;c.clear();return c;}bool operator < (const bign& b){if (len != b.len) return len < b.len;for (int i = len - 1; i >= 0; i--)if (d[i] != b.d[i]) return d[i] < b.d[i];return false;}bool operator <= (const bign& b){if (len != b.len) return len < b.len;for (int i = len - 1; i >= 0; i--)if (d[i] != b.d[i]) return d[i] < b.d[i];return true;}bool operator > (const bign& b){if (len != b.len) return len > b.len;for (int i = len - 1; i >= 0; i--)if (d[i] != b.d[i]) return d[i] > b.d[i];return false;}bool operator >= (const bign& b){if (len != b.len) return len > b.len;for (int i = len - 1; i >= 0; i--)if (d[i] != b.d[i]) return d[i] > b.d[i];return true;}bign operator+=(const bign& b){*this = *this + b;return *this;}bign operator-=(const bign& b){*this = *this - b;return *this;}bign operator*=(const bign& b){*this = *this * b;return *this;}void print(){for (int i = len - 1; i >= 0; i--) std::cout << d[i];cout << '\n';}string str(){string res = "";for (int i = 0; i < len; i++) res = (char)(d[i] + '0') + res;return res;}
};
istream& operator >>(istream& in, bign& x)
{string s;in >> s;x = s.c_str();return in;
}
ostream& operator <<(ostream& out, bign& x)
{out << x.str();return out;
};
bign max(bign a,bign b)
{return a>b?a:b;
}
//==========================================================
constexpr int N=710;
int h[N],e[2*N],ne[2*N],idx;
void add(int a,int b){e[idx]=b,ne[idx]=h[a],h[a]=idx++;}
int sz[N],n;
bign f[N][N],ans;
void dfs(int u,int fa)
{f[u][1]=1;sz[u]=1;for(int i=h[u];i!=-1;i=ne[i]){int v=e[i];if(v==fa) continue;dfs(v,u);bign now=1;for(int j=1;j<=sz[v];j++) now=max(now,f[v][j]*bign(j));for(int j=sz[u];j>=1;j--){for(int k=sz[v];k>=1;k--)f[u][j+k]=max(f[u][j+k],f[u][j]*f[v][k]);  f[u][j]=f[u][j]*now;    }sz[u]+=sz[v];}for(int j=1;j<=sz[u];j++)ans=max(ans,f[u][j]*bign(j));
}
int main()
{cin>>n;memset(h,-1,sizeof h);for(int i=1;i<n;i++){int u,v;cin>>u>>v;add(u,v),add(v,u);}dfs(1,0);ans.print();return 0;
}

一下午搞了个这个题,顺便整理了一下高精度摸吧yep!
要加油哦~

codeforce23 E. Tree(高精度+树形dp)相关推荐

  1. Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

    题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...

  2. 【牛客 - 303K第十五届浙江大学宁波理工学院程序设计大赛(同步赛)】Technology Tree(树形dp,tricks)

    题干: 在星际争霸(StarCraft)中,有3个种族.对于任意一个种族,他们的建筑建造都是有一个顺序的.这个顺序正好是一个树形结构,我们称之为"科技树"(Technology t ...

  3. CF1120D Power Tree(树形DP/构造+差分+最小生成树)

    解法一:树形DP 个人觉得这个方法是比较可能想到的,但是输出方案很恶心 先转换题意:"无论怎样规定叶子的初始点权,都可以通过操作你选择的点来让所有叶子的点权清空"意味着每个叶子节点 ...

  4. bzoj4987: Tree(树形dp)

    Description 从前有棵树. 找出K个点A1,A2,-,Ak. 使得∑dis(AiAi+1),(1<=i<=K-1)最小. Input 第一行两个正整数n,k,表示数的顶点数和需要 ...

  5. Codeforces1153D-Serval and Rooted Tree(树形dp)

    原题链接:http://codeforces.com/problemset/problem/1153/D 题目原文: D. Serval and Rooted Tree time limit per ...

  6. D. Nastia Plays with a Tree(树形dp)

    https://codeforces.com/contest/1521/problem/D 思路: 把树上的单链初始时每个点是一个链.然后类似树形dp递归.递归过程中模拟链的断开,拼凑新的链头链尾. ...

  7. 【CF1646D】D. Weight the Tree(树形dp、贪心)

    加权树 题意: 给定一颗树,让你给树上的点赋予权值.定义一个点的权值等于其所有相邻节点的权重之和时,这个点就是 good. 你需要找到一种赋值方法,使得树中 good 点数最多,同时所有顶点的权重总和 ...

  8. 2020牛客多校9:B. Groundhog and Apple Tree(树形DP + 分类讨论 + 贪心)

    题目大意:有一棵苹果树,每个节点有一个苹果,吃掉 uuu 点的苹果能获得 aua_uau​点 HP,经过第 iii 条边需要消耗 wiw_iwi​ HP,在原地等待一秒可以获得 111 HP,每条边只 ...

  9. 【CodeForces - 1153D】Serval and Rooted Tree(树形dp)

    题干: Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on ...

最新文章

  1. 在一家开源公司工作的体验!
  2. userdel、usermod、newgrp、chown、chgrp、手动添加用户、umask
  3. 新思科技助力IBM将AI计算性能提升1000倍
  4. 050_学习的CSS属性
  5. 读书笔记 -《深入理解计算机系统》2.1
  6. 如何使用SSL pinning来使你的iOS APP更加安全
  7. Spring 整合 RocketMQ
  8. 面试必胜的九大素质八大能力
  9. 根据F12在页面中调整div的大小
  10. CSS边框border属性
  11. U盘启动BIOS设置
  12. 苦逼程序猿的求职经历
  13. Word中所有公式转为特定文本
  14. 苹果cms怎么更换模板logo详细教程
  15. 应届生怎么样成为产品经理?
  16. 错误提示 - Procmon.exe - 无法找到入口(InitializeSRWLock)
  17. 通信原理第6章节-数字基带传输系统
  18. jenkins+git+maven+docker持续集成部署
  19. 上海养老保险(社保)缴费记录查询
  20. linux如何获取root权限

热门文章

  1. c语言歌手预测成绩,5个裁判可以对10个歌手进行打分,计算各个歌手的最终得分排列...
  2. 哈希表(散列表)基础概念与经典题目(Leetcode题解-Python语言)之上——原理与设计
  3. [设计模式]简单工厂和工厂方法模式适用场景
  4. LeetCode 24两两交换链表中的节点-中等
  5. [蓝桥杯2016初赛]剪邮票-dfs+next_permutation(好题)
  6. cubemx lan8720模块_通过STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现
  7. word List 13
  8. 数据结构---多源最短路径
  9. OpenJudge:熄灯问题
  10. Codeforces Round #296 (Div. 1) D. Fuzzy Search FFT匹配字符串