1 条题解
-
0
自动搬运
来自洛谷,原作者为

孤叶残影
有朋自远方来,虽远必诛搬运于
2025-08-24 21:24:47,当前版本为作者最后更新于2019-11-26 10:35:22,作者可能在搬运后再次修改,您可在原文处查看最新版自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多
以下是正文
注意:我认为题解对dp[u][j]理解有误
若dp[u][j]表示以U为根联通块大小为j的最大贡献的话,
最后dp[u][0]为何又要乘以i呢(已经是最大贡献了再乘以该联通块大小)
若dp[u][j]表示以U为根联通块大小为j的最大贡献/j的话,
最后一个方程说通了,那么第一个方程dp[u][j]=max(dp[u][k]*dp[v][j-k])又怎么解释呢,
还有最大贡献/联通块大小的数学意义有是什么?
每个点的平均贡献吗?
那么吧两个联通块平均值乘起来就能得到总联通块的平均点贡献吗?
我认为,dp[u][j]表示以u为根节点,除了包含u的大小为j的联通块之外的其他联通块的最大贡献
dp[u][j]=max(dp[u][k]*dp[v][j-k])表示,
在以u为根节点的树上选走k个后剩余联通块的贡献
dp[u][0]=max(dp[u][j]*j) 并不是表示取0个,
只不过0这一维代替再开一个数组。 它表示整个u树的最大贡献,
因为前面未将dp[u][j]算入,则乘上它的贡献!

例子:合并联通块(A到G,下面简称A)和联通块(c到E,简称C),因为是dfs遍历dp,则在此时dp[A][4]只更新的左子树,右子树还未更新,及dp[A][4]表示H的贡献及为1,dp[C][2]同理为I和F的贡献为1,则dp[A][6]为H,F,I总贡献1,最后A树的贡献为dp[A][6]*6(合并后G到E的贡献为其联通块大小)
最后再注意:高精需开short,否则会MLE
#include <cstdio> #include <iostream> #include <cstring> #define N 700 using namespace std; int n, eid; short sz[N+5], head[N+5]; struct Edge { int next, to; }e[2*N+5]; struct bign { static const int maxn = 120; short d[maxn+5]; short len; void clean() { while(len > 1 && !d[len-1]) len--; } bign() { memset(d, 0, sizeof(d)); len = 1; } bign(int num) { *this = num; } bign(char* num) { *this = num; } bign operator = (const char* num) { memset(d, 0, sizeof(d)); len = strlen(num); for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0'; clean(); return *this; } bign operator = (int num) { char s[20]; sprintf(s, "%d", num); *this = s; return *this; } bign operator + (const bign& b) { bign c = *this; int i; for(i = 0; i < b.len; i++) { c.d[i] += b.d[i]; if (c.d[i] > 9) c.d[i] %= 10, c.d[i+1]++; } while (c.d[i] > 9) c.d[i++] %= 10, c.d[i]++; c.len = max(len, b.len); if (c.d[i] && c.len <= i) c.len = i+1; return c; } bign operator - (const bign& b) { bign c = *this; int i; for(i = 0; i < b.len; i++) { c.d[i] -= b.d[i]; if (c.d[i] < 0) c.d[i] += 10, c.d[i+1]--; } while (c.d[i] < 0) c.d[i++] += 10, c.d[i]--; c.clean(); return c; } bign operator * (const bign& b) const { int i, j; bign c; c.len = len + b.len; for(j = 0; j < b.len; j++) for(i = 0; i < len; i++) c.d[i+j] += d[i]*b.d[j]; for(i = 0; i < c.len-1; i++) c.d[i+1] += c.d[i]/10, c.d[i] %= 10; c.clean(); return c; } bign operator / (const bign& b) { int i, j; bign c = *this, a = 0; for(i = len - 1; i >= 0; i--) { a = a*10 + d[i]; for (j = 0; j < 10; j++) if (a < b*(j+1)) break; c.d[i] = j; a = a - b*j; } c.clean(); return c; } bign operator % (const bign& b) { int i, j; bign a = 0; for(i = len - 1; i >= 0; i--) { a = a*10+d[i]; for(j = 0; j < 10; j++) if (a < b*(j+1)) break; a = a-b*j; } return a; } bign operator += (const bign& b) { *this = *this+b; return *this; } bool operator <(const bign& b) const { 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) const { return b < *this; } bool operator <= (const bign& b) const { return !(b < *this); } bool operator >= (const bign& b) const { return !(*this < b); } bool operator != (const bign& b) const { return b < *this || *this < b; } bool operator == (const bign& b) const { return !(b < *this) && !(b > *this); } string str() const { char s[maxn] = {}; for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0'; return s; } }f[N+5][N+5]; istream& operator >> (istream& in, bign& x) { string s; in >> s; x = s.c_str(); return in; } ostream& operator << (ostream& out, const bign& x) { out << x.str(); return out; } void addEdge(int u, int v) { e[++eid].next = head[u]; e[eid].to = v; head[u] = eid; } void dp(int u, int fa) { sz[u] = 1, f[u][0] = f[u][1] = 1; for(int i = head[u]; i; i = e[i].next) { int v = e[i].to; if(v == fa) continue; dp(v, u); sz[u] += sz[v]; for(int j = sz[u]; j >= 1; --j) { for(int k = min(j, sz[u]-sz[v]); k >= max(1, j-sz[v]); --k) { f[u][j] = max(f[u][j], f[u][k]*f[v][j-k]); } } } for(int i = 1; i <= sz[u]; ++i) f[u][0] = max(f[u][0], f[u][i]*i); } int main() { scanf("%d",&n); for(int i = 1, x, y; i <= n-1; ++i) { scanf("%d %d",&x,&y); addEdge(x, y); addEdge(y, x); } dp(1, 0); cout << f[1][0] << endl; return 0; }
- 1
信息
- ID
- 405
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- 6
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者