题意:

t组输入,每组数据中n个节点构成一棵树,然后给你n-1条边。给你一个m,然后给你m个k的素数因子,你需要给这n-1条边都赋一个权值,这n-1条边的权值之积应该等于k。如果k的素数因子数量小于n-1,那可以使用1来填充

然后我们定义F(x,y)为节点x到节点y的路径上所有边的和

我们要求出来所有任意两点之间的F(x,y),然后把所有F(x,y)加起来输出,求最大结果是多少,结果取余1e9+7

题解:

因为我们要使

这个尽可能大,所以肯定要按那一条边使用次数最多,我们就把最大那个素数因子给这一条边,这样得到的结果肯定最大

怎么处理每一条边的使用次数,可以使用dfs遍历一遍就可以了

dfs过程中如果遇到叶节点,那么与叶节点相连这条边的使用次数也就是n-1,例如叶节点是1,那么节点1与2,3,4...n这些点构成的路径会经过这条边n-1次

如果不是叶节点,我们首先要在dfs过程中记录一下,这个节点有多少子节点,设为ans,然后ans*(n-ans)就是对与这个节点相连那条边的使用次数

之后再处理一下素数因子,如果素数因子小于n-1,那么就需要补加上n-m+1个1

如果素数因子大于n-1,那么就让多的素数因子乘起来变成一个

dfs的根节点,就随便找一个叶节点就行

代码:

#include<stack>
#include<queue>
#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
const int mod=1e9+7;
const double eps=1e-8;
const int INF = 0x3f3f3f3f;
vector<ll>w[maxn],L;
ll p[maxn],n,root,m;
void add_edge(ll x,ll y)
{
w[x].push_back(y);
w[y].push_back(x);
}
ll dfs(ll x,ll fa)
{
ll len=w[x].size(),ans=0;
if(len==1 && x!=root)
{
L.push_back(n-1);
return 1;
}
for(ll i=0; i<len; ++i)
{
ll y=w[x][i];
if(y==fa) continue;
ll temp=dfs(y,x);
ans+=temp;
}
ans++;
//printf("%lld %lld*****\n",x,1);
//printf("%lld***%lld\n",ans,ans*(n-ans));
if(x!=root)
{ L.push_back(ans*(n-ans));
}
return ans;
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll t;
cin>>t;
while(t--)
{
ll m,sum=0;
//num=1;
root=1;
L.clear();
//scanf("%lld",&n);
cin>>n;
for(ll i=1; i<=n; ++i)
w[i].clear();
for(ll i=1; i<n; ++i)
{ ll x,y;
//scanf("%lld%lld",&x,&y);
cin>>x>>y;
add_edge(x,y);
}
//scanf("%lld",&m);
cin>>m;
for(ll i=1; i<=m; ++i)
{
//scanf("%lld",&p[i]);
cin>>p[i];
}
for(ll i=1; i<=n; ++i)
{
if(w[i].size()==1)
{
root=i;
break;
}
}
dfs(root,-1);
sort(p+1,p+1+m,greater<ll>());
sort(L.begin(),L.end(),greater<ll>());
ll d=0;
if (m > n - 1)
{
d = m - n + 1;
for (ll i = 1; i <= d; i++)
{
p[i + 1] = p[i + 1] * p[i] % mod;
}
}
if (m < n - 1)
{
for (ll i = m + 1; i <= n - 1; i++)
{
p[i] = 1;
}
}//printf("*********\n");
sum=0;
for(ll i=1;i<n;++i)
{
//printf("%lld %lld\n",que[i],p[i]);
sum=sum+L[i-1]*p[i+d];
sum = (sum + mod) % mod;
}
cout<<sum<<endl;
}
return 0;
}

Codeforces Round #665 (Div. 2) D - Maximum Distributed Tree dfs贡献记录的更多相关文章

  1. Codeforces Round #665 (Div. 2) D. Maximum Distributed Tree (dfs计数,树)

    题意:给你含有\(n\)个节点,\(n-1\)条边的树,以及\(m\)个质数和\(1\),你需要在这\(m\)个质数和一个\(1\)选择数(质数只能选一次,\(1\)可以多选)给\(n-1\)条边赋值 ...

  2. Codeforces Round #665 (Div. 2) D. Maximum Distributed Tree 题解(贪心+易错)

    题目链接 题目大意 给你一课树,要你给每一条边分权值,每条边的权值大于0,他们的乘积等于k,而且要使得n-1条边1的数量尽可能少,定义 f(u,v)为u到v的边权和求 \(\max \sum_{i=1 ...

  3. Codeforces Round #665 (Div. 2)

     Codeforces Round #665 (Div. 2)  A. Distance and Axis 如果\(B\)在\(O\)左边,那么只能是定值\(OA\) 如果\(B\)在\(OA\)中间 ...

  4. Codeforces Round #665 (Div. 2) 题解

    Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...

  5. Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造

    B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...

  6. Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序

    B. Maximum Submatrix 2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  7. Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数

    B. Maximum Value Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/prob ...

  8. Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)

     E. Maximum Matching 题目链接:https://codeforces.com/contest/1038/problem/E 题意: 给出n个项链,每条项链左边和右边都有一种颜色(范 ...

  9. Codeforces Round #172 (Div. 2) D. Maximum Xor Secondary 单调栈应用

    http://codeforces.com/contest/281/problem/D 要求找出一个区间,使得区间内第一大的数和第二大的数异或值最大. 首先维护一个单调递减的栈,对于每个新元素a[i] ...

随机推荐

  1. Nginx 安装与配置教程

    标签: Nginx Linux Windows 配置 描述: Ubuntu 下以及 Windows 下 Nginx 的配置:配置详解:有关 Nginx 如何配置 Nginx 在 Ubuntu 下的安装 ...

  2. Python基础语法5-控制流语句

  3. 诸葛 VS 庞统,拿下 Paxos 共识算法

    前言 分布式确实是一个有趣的话题,只要你留心观察,分布式在生活中无处不在. 悟空哥最开始学习分布式是从一篇非常用心写的技术征文开始的,而且这篇文章获得了征文第一名,在此感谢掘金社区提供的平台.想学习的 ...

  4. Gulp4.0入门和实战

    gulp4.0入门和实战 我最近遇到需要优化web的性能的任务,然后就捣鼓了一些对资源文件优化压缩的方案.由于之前的项目中有使用到gulp,所以在需要处理的web项目中也优先使用这个技术. 先聊聊gu ...

  5. 笔记 | 吴恩达新书《Machine Learning Yearning》

    这本书共112页,内容不多,偏向于工程向,有很多不错的细节,在此记录一下. 0 书籍获取 关注微信公众号"机器学习炼丹术",回复[MLY]获取pdf 1 测试集与训练集的比例 2 ...

  6. cisco思科交换机终端远程ssh另一端报错:% ssh connections not permitted from this terminal

    故障现象: XSJ-GH10-C3750->ssh 58.64.xx.xx% ssh connections not permitted from this terminal 解决办法: 原因: ...

  7. git的使用学习笔记4--创建分支

    1.在git上新建分支 查看本地分支 git branch 查看远程分支 git branch -a 创建一个分支 git checkout -b branch1 再次查看远程分支可以看到该分支 2. ...

  8. ensure that both new and old access_token values are available within five minutes, so that third-party services are smoothly transitioned.

    WeChat public doc https://developers.weixin.qq.com/doc/offiaccount/en/Basic_Information/Get_access_t ...

  9. Convert a string into an ArrayBuffer

    https://github.com/mdn/dom-examples/blob/master/web-crypto/import-key/spki.js How to convert ArrayBu ...

  10. (转载)微软数据挖掘算法:Microsoft 决策树分析算法(1)

    微软数据挖掘算法:Microsoft 目录篇 介绍: Microsoft 决策树算法是分类和回归算法,用于对离散和连续属性进行预测性建模. 对于离散属性,该算法根据数据集中输入列之间的关系进行预测. ...