codeforce23 E. Tree(高精度+树形dp)
生活随笔
收集整理的這篇文章主要介紹了
codeforce23 E. Tree(高精度+树形dp)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
E. Tree
狀態表示:fu,jf_{u,j}fu,j?表示以uuu節點的子樹,uuu所在連通塊大小為jjj時,并且沒有算上uuu連通塊的貢獻的最大值
狀態計算:
對于一棵子樹vvv來說,顯然可以有兩種情況
- uuu節點與vvv節點不連通:fu,j=fu,j×max?[fv,1→szv×(1→szv)]f_{u,j}=f_{u,j}×\max[f_{v,1\to sz_{v}}×(1\to sz_v)]fu,j?=fu,j?×max[fv,1→szv??×(1→szv?)]
- uuu節點與vvv節點連通:fu,j+k=fu,j×fv,kf_{u,j+k}=f_{u,j}×f_{v,k}fu,j+k?=fu,j?×fv,k?
而答案就是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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全自动纸盒贴窗机给包装企业带来了哪些好处
- 下一篇: 美媒:拜登的对华电脑芯片战争伤害美国人