Codeforces Beta Round #23 E. Tree(DP+高精度)

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.

Input

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, aibi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree.

Output

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.

Examples
input
5
1 2
2 3
3 4
4 5
output
6
input
8
1 2
1 3
2 4
2 5
3 6
3 7
6 8
output
18
input
3
1 2
1 3
output

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]);
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值