CodeForces 1401D Maximum Distributed Tree (数论,树)

You are given a tree that consists of nn nodes. You should label each of its n−1n−1 edges with an integer in such way that satisfies the following conditions:

  • each integer must be greater than 00;
  • the product of all n−1n−1 numbers should be equal to kk;
  • the number of 11-s among all n−1n−1 integers must be minimum possible.

Let's define f(u,v)f(u,v) as the sum of the numbers on the simple path from node uu to node vv. Also, let ∑i=1n−1∑j=i+1nf(i,j)∑i=1n−1∑j=i+1nf(i,j) be a distribution index of the tree.

Find the maximum possible distribution index you can get. Since answer can be too large, print it modulo 109+7109+7.

In this problem, since the number kk can be large, the result of the prime factorization of kk is given instead.

Input

The first line contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.

The first line of each test case contains a single integer nn (2≤n≤1052≤n≤105) — the number of nodes in the tree.

Each of the next n−1n−1 lines describes an edge: the ii-th line contains two integers uiui and vivi (1≤ui,vi≤n1≤ui,vi≤n; ui≠viui≠vi) — indices of vertices connected by the ii-th edge.

Next line contains a single integer mm (1≤m≤6⋅1041≤m≤6⋅104) — the number of prime factors of kk.

Next line contains mm prime numbers p1,p2,…,pmp1,p2,…,pm (2≤pi<6⋅1042≤pi<6⋅104) such that k=p1⋅p2⋅…⋅pmk=p1⋅p2⋅…⋅pm.

It is guaranteed that the sum of nn over all test cases doesn't exceed 105105, the sum of mm over all test cases doesn't exceed 6⋅1046⋅104, and the given edges for each test cases form a tree.

Output

Print the maximum distribution index you can get. Since answer can be too large, print it modulo 109+7109+7.

Example

input

Copy

3
4
1 2
2 3
3 4
2
2 2
4
3 4
1 3
3 2
2
3 2
7
6 1
2 3
4 6
7 3
5 1
3 6
4
7 5 13 3

output

Copy

17
18
286

Note

In the first test case, one of the optimal ways is on the following image:

 

In this case, f(1,2)=1f(1,2)=1, f(1,3)=3f(1,3)=3, f(1,4)=5f(1,4)=5, f(2,3)=2f(2,3)=2, f(2,4)=4f(2,4)=4, f(3,4)=2f(3,4)=2, so the sum of these 66 numbers is 1717.

In the second test case, one of the optimal ways is on the following image:

 

In this case, f(1,2)=3f(1,2)=3, f(1,3)=1f(1,3)=1, f(1,4)=4f(1,4)=4, f(2,3)=2f(2,3)=2, f(2,4)=5f(2,4)=5, f(3,4)=3f(3,4)=3, so the sum of these 66 numbers is 1818.

 

题解:容易想到,计算出每条边的访问次数,再将质因数按一定方法赋给边权中。由于每两个点之间路径都要通过一次,对于某一条边,它的经过次数是它两端子树节点数的乘积。因此建树,dfs求出每个结点的size,另一端数量就是(n-size)。由于建树用到双向边,dfs时记录深度,取深度大的一端的size。

边权的最优解证明在下面

注意溢出!!!!

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string.h>
using namespace std;
const int maxn=100002,p=1000000007;
long long  e[maxn*2][2],last[maxn*2],nexts[maxn*2],f[maxn],dep[maxn],k[maxn];
long long  w[maxn];
bool v[maxn];
int n,m,sum;
long long ans;

void add(int x,int y)
{
	++sum;
	e[sum][0]=x;
	e[sum][1]=y;
	nexts[sum]=last[x];
	last[x]=sum;
}

void build(int x,int y)
{
	if ((nexts[last[x]]==0)&&(x!=1))
	{
		exit;
	} 
	dep[x]=y;
	v[x]=true;
	int i=last[x];
	while (i!=0)
	{
		if (!v[e[i][1]])
		{
			build(e[i][1],y+1);
			f[x]+=f[e[i][1]];
		}
		i=nexts[i];
	}
}

void init()
{
	cin>>n;
	ans=0;
	memset(w,0,sizeof(w));
	memset(f,0,sizeof(f));
	memset(nexts,0,sizeof(nexts));
	memset(last,0,sizeof(last));
	sum=0;
	for (int i=1;i<=n-1;i++)
	{
		int x,y;
		cin>>x>>y;
		add(x,y);
		add(y,x);
		v[i]=false;
		f[i]=1; 
	}
	v[n]=false;
	f[n]=1;
	build(1,1);
	cin>>m;
	for (int i=1;i<=m;i++)
	{
		cin>>k[i];
	}
	sort(k+1,k+m+1);
}




int main()
{
	int t;
	cin>>t;
	for (int tt=1;tt<=t;tt++)
	{
		init();
		for (int i=1;i<=sum;i++)
		{
			if (dep[e[i][0]]>dep[e[i][1]]) continue;
			++w[0];
			w[w[0]]=f[e[i][1]]*(n-f[e[i][1]]);
		}
		sort(w+1,w+n);
		if (m<n-1)
		{
			int i=m,j=n-1;
			while (i>0)
			{
				ans+=(k[i]%p)*(w[j]%p);
				ans=ans%p;
				i-=1;j-=1;
			}
			while (j>0) 
			{
				ans+=w[j];
				ans=ans%p;
				j-=1;
			}
		}
		if (m>=n-1)
		{
			int i=m;
			long long pp=1;
			while (i>=n-1)
			{
				pp*=k[i];
				pp=pp%p;
				i-=1;	
			}
			ans=(pp*w[n-1])%p;
			for (int i=n-2;i>=1;i--)
			{
				
				ans+=(w[i]%p)*(k[i]%p);
				ans=ans%p;
			}
		}
		cout<<ans<<endl;
	}
	
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值