题目链接

题目大意

给你一课树,要你给每一条边分权值,每条边的权值大于0,他们的乘积等于k,而且要使得n-1条边1的数量尽可能少,定义

f(u,v)为u到v的边权和求 \(\max \sum_{i=1}^{i=n}\sum_{j=1}^{j=n} f(i,j)\)

k为m个质因子的乘积

题目思路

这显然是一个求贡献的裸题,但是里面有易错点

1:sort前不要先取模

2:还有要区分m可能比n-1大(太坑了

代码

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e5+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-10;
int n, m, a[maxn];
ll sz[maxn], p[maxn];
int head[maxn],cnt;
struct node{
int to, next;
}e[maxn<<1];
void add(int u,int v){
e[++cnt] = {v, head[u]};
head[u] = cnt;
}
void dfs(int son,int fa){
sz[son] = 1;
for (int i = head[son]; i;i=e[i].next){
if(e[i].to==fa) continue;
dfs(e[i].to,son);
sz[son] += sz[e[i].to];
}
}
void init(){
cnt = 0;
for (int i = 1; i <= n;i++){
head[i] = sz[i] = 0;
}
}
signed main(){
int _;scanf("%d", &_);
while(_--){
scanf("%d",&n);
init();
for (int i = 1,u,v; i <= n - 1;i++){
scanf("%d%d", &u, &v);
add(u, v), add(v, u);
}
scanf("%d", &m);
for (int i = 1; i <= m;i++){
scanf("%lld", &p[i]);
}
sort(p + 1, p + 1 + m);//从大到小
reverse(p + 1, p + 1 + m);
dfs(1,1);
for (int i = 1; i <= n;i++){//先不要取模
sz[i] = (sz[i]) * (n - sz[i]);
}
sort(sz + 1, sz + 1 + n);//从大到小
reverse(sz + 1, sz + 1 + n);
ll ans = 0;
if(n-1>=m){
for (int i = 1; i <= n-1;i++){
if(i<=m){
ans =(ans+ sz[i]%mod * p[i])%mod;
}else{
ans =(ans+ sz[i])%mod;
}
}
}else{
for (int i = 2; i <= m;i++){
if(i<=m-n+2){
p[1] = p[1] * p[i]%mod;
}else{
p[i-(m-n+2)+1] = p[i];
}
}
for (int i = 1; i <= n-1; i++){
ans = (ans + sz[i] * p[i]) % mod;
}
}
printf("%lld\n", ans);
}
return 0;
}

Codeforces Round #665 (Div. 2) D. Maximum Distributed Tree 题解(贪心+易错)的更多相关文章

  1. Codeforces Round #665 (Div. 2) D - Maximum Distributed Tree dfs贡献记录

    题意: t组输入,每组数据中n个节点构成一棵树,然后给你n-1条边.给你一个m,然后给你m个k的素数因子,你需要给这n-1条边都赋一个权值,这n-1条边的权值之积应该等于k.如果k的素数因子数量小于n ...

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

    题意:给你含有\(n\)个节点,\(n-1\)条边的树,以及\(m\)个质数和\(1\),你需要在这\(m\)个质数和一个\(1\)选择数(质数只能选一次,\(1\)可以多选)给\(n-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 #524 (Div. 2)(前三题题解)

    这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...

  6. 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/ ...

  7. 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 ...

  8. 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 ...

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

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

随机推荐

  1. Iperius Backup Full--小中企业简单自动备份的实用工具

    从事IT行业几个年头了,一直以来发现备份这个词是十分特殊的.无论是事业国有大企央企还是个人爱好者,小型工作室,中小企业. 对于备份都是明确知道十分重要,但在正在实施起来会因为投入,领导重视程度,实施管 ...

  2. sqlsugar入门(2)-C#方法与sugar自定义函数的区别

    1.使用tostring获取当天数据 var list = ssc.Queryable<Student>().Where(o => o.CreateTime.Value.ToStri ...

  3. 04 . Go+Vue开发一个线上外卖应用(用户名密码和图形验证码)

    图形化验证码生成和验证 功能介绍 在使用用户名和密码登录功能时,需要填写验证码,验证码是以图形化的方式进行获取和展示的. 验证码使用原理 验证码的使用流程和原理为:在服务器端负责生成图形化验证码,并以 ...

  4. [论文解读] 阿里DIEN整体代码结构

    [论文解读] 阿里DIEN整体代码结构 目录 [论文解读] 阿里DIEN整体代码结构 0x00 摘要 0x01 文件简介 0x02 总体架构 0x03 总体代码 0x04 模型基类 4.1 基本逻辑 ...

  5. DP百题练(二)

    目录 DP百题练(二) 区间 DP NOI1995 石子合并 IOI1998 Polygon CH5302 金字塔 USACO06FEB Treats for the Cows G/S LG1043 ...

  6. AWS SDK 使用说明

    AWS 的Python SDK包名为 boto3, 可以使用命令pip install boto3安装使用 BOTO3中的基本概念 boto3提供了两个级别的接口来访问AWS服务:High Level ...

  7. 【Luogu】P6232 [eJOI2019]挂架 题解

    这道题跟CSP/S 2019 D1T1有点像. 我们先来模拟一下 \(n=4\) 的情况, 不难得出,最后的衣架挂钩顺序: 下标: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...

  8. How to: Debug X++ Code Running in .NET Business Connector [AX 2012]

    This topic has not yet been rated - Rate this topic  http://msdn.microsoft.com/EN-US/library/bb19006 ...

  9. angularJS 小记

    刚刚接触angularJS,网上学习了一遍菜鸟教程(http://www.runoob.com/angularjs/angularjs-tutorial.html),做了些基础知识的笔记. Angul ...

  10. 剑指29:最小的k个数

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. class Solution {public:    vector& ...