Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree.
The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree.
Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges.
5 1 2 2 3 3 4 4 5
6
8 1 2 1 3 2 4 2 5 3 6 3 7 6 8
18
3 1 2 1 3
3
题意:给定一个n结点的树,删去若干边,要求最大化得到的所有连通块大小的乘积,n <= 700。
分析:dp[i][j]表示以i为根的子树所在的联通块大小为j时其余联通块的最大乘积,注意结果要用高精度,我压了四位。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<ctime>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define N 705
#define MAXL 60
using namespace std;
struct BigNum
{
int num[MAXL];
int len;
BigNum()
{
memset(num,0,sizeof(num));
len = 0;
}
}dp[N][N],Max[N];
int n,u,v,size[N];
vector<int> G[N];
BigNum Mul(BigNum a,BigNum b)
{
int len = 0;
BigNum c;
for(int i = 0;i < a.len;i++)
for(int j = 0;j < b.len;j++)
{
c.num[i+j] += a.num[i]*b.num[j];
if(c.num[i+j] >= 10000)
{
c.num[i+j+1] += c.num[i+j]/10000;
c.num[i+j] %= 10000;
}
}
len = a.len + b.len;
while(c.num[len-1] == 0 && len > 1) len--;
c.len = len;
return c;
}
BigNum Mul(BigNum a,int b)
{
int len = a.len;
BigNum c;
if(!b) return c;
for(int i = 0;i < len;i++)
{
c.num[i] += a.num[i]*b;
if(c.num[i] >= 10000)
{
c.num[i+1] += c.num[i]/10000;
c.num[i] %= 10000;
}
}
while(c.num[len] > 0)
{
c.num[len+1] = c.num[len]/10000;
c.num[len++] %= 10000;
}
c.len = len;
return c;
}
BigNum max(BigNum a,BigNum b)
{
if(a.len < b.len) return b;
if(a.len > b.len) return a;
for(int i = a.len-1;i >= 0;i--)
{
if(a.num[i] < b.num[i]) return b;
if(a.num[i] > b.num[i]) return a;
}
return b;
}
void dfs(int u,int fa)
{
size[u] = 1;
dp[u][1].len = 1;
dp[u][1].num[0] = 1;
for(int v : G[u])
if(v != fa)
{
dfs(v,u);
size[u] += size[v];
}
for(int v : G[u])
if(v != fa)
for(int i = size[u];i;i--)
if(dp[u][i].len)
{
for(int j = 1;j <= size[v];j++)
if(dp[v][j].len)
dp[u][i+j] = max(dp[u][i+j],Mul(dp[u][i],dp[v][j]));
dp[u][i] = Mul(dp[u][i],Max[v]);
}
for(int i = size[u];i;i--) Max[u] = max(Max[u],Mul(dp[u][i],i));
}
int main()
{
scanf("%d",&n);
for(int i = 1;i < n;i++)
{
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1,-1);
for(int i = Max[1].len;i;i--)
if(i != Max[1].len)
{
if(Max[1].num[i-1] < 1000)
{
printf("0");
if(Max[1].num[i-1] < 100)
{
printf("0");
if(Max[1].num[i-1] < 10) printf("0");
}
}
printf("%d",Max[1].num[i-1]);
}
else printf("%d",Max[1].num[i-1]);
}