Codeforces1401 D. Maximum Distributed Tree(DFS)

2024-04-16 00:09

本文主要是介绍Codeforces1401 D. Maximum Distributed Tree(DFS),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

each integer must be greater than 0;
the product of all 𝑛−1 numbers should be equal to 𝑘;
the number of 1-s among all 𝑛−1 integers must be minimum possible.
Let’s define 𝑓(𝑢,𝑣) as the sum of the numbers on the simple path from node 𝑢 to node 𝑣. Also, let ∑𝑖=1𝑛−1∑𝑗=𝑖+1𝑛𝑓(𝑖,𝑗) 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+7.

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

Input
The first line contains one integer 𝑡 (1≤𝑡≤100) — the number of test cases.

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

Each of the next 𝑛−1 lines describes an edge: the 𝑖-th line contains two integers 𝑢𝑖 and 𝑣𝑖 (1≤𝑢𝑖,𝑣𝑖≤𝑛; 𝑢𝑖≠𝑣𝑖) — indices of vertices connected by the 𝑖-th edge.

Next line contains a single integer 𝑚 (1≤𝑚≤6⋅104) — the number of prime factors of 𝑘.

Next line contains 𝑚 prime numbers 𝑝1,𝑝2,…,𝑝𝑚 (2≤𝑝𝑖<6⋅104) such that 𝑘=𝑝1⋅𝑝2⋅…⋅𝑝𝑚.

It is guaranteed that the sum of 𝑛 over all test cases doesn’t exceed 105, the sum of 𝑚 over all test cases doesn’t exceed 6⋅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+7.

Example
inputCopy
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
outputCopy
17
18
286
Note
In the first test case, one of the optimal ways is on the following image:

In this case, 𝑓(1,2)=1, 𝑓(1,3)=3, 𝑓(1,4)=5, 𝑓(2,3)=2, 𝑓(2,4)=4, 𝑓(3,4)=2, so the sum of these 6 numbers is 17.

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

In this case, 𝑓(1,2)=3, 𝑓(1,3)=1, 𝑓(1,4)=4, 𝑓(2,3)=2, 𝑓(2,4)=5, 𝑓(3,4)=3, so the sum of these 6 numbers is 18.

思路:
只要算出每个边的贡献,再把质因数排个序依次匹配即可。
如果质因数小于n-1,那么添加1。
如果大于n-1,那么合并最后的几个质因数。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>using namespace std;typedef long long ll;
const int maxn = 4e5 + 7;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;int head[maxn],nex[maxn],to[maxn],tot;
ll dp[maxn],a[maxn];
ll siz[maxn];
int n;void add(int x,int y) {to[++tot] = y;nex[tot] = head[x];head[x] = tot;
}void init() {tot = 0;for(int i = 1;i <= n;i++) {head[i] = dp[i] = 0;}
}void dfs(int x,int fa) {siz[x] = 1;for(int i = head[x];i;i = nex[i]) {int v = to[i];if(v == fa) continue;dfs(v,x);siz[x] += siz[v];dp[(i + 1) / 2] = 1ll * siz[v] * (n - siz[v]);}
}int main() {int T;scanf("%d",&T);while(T--) {scanf("%d",&n);init();for(int i = 1;i < n;i++) {int x,y;scanf("%d%d",&x,&y);add(x,y);add(y,x);}int m;scanf("%d",&m);for(int i = 1;i <= m;i++) {scanf("%lld",&a[i]);}dfs(1,-1);while(m < n - 1) {a[++m] = 1;}sort(a + 1,a + 1 + m);sort(dp + 1,dp + 1 + n - 1);if(m > n - 1) {for(int j = n;j <= m;j++) {a[n - 1] = a[n - 1] * a[j] % mod;}}ll ans = 0;for(int i = 1;i <= n - 1;i++) {ans = (ans + dp[i] * a[i] % mod) % mod;}printf("%lld\n",ans);}return 0;
}

这篇关于Codeforces1401 D. Maximum Distributed Tree(DFS)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907303

相关文章

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

poj 3050 dfs + set的妙用

题意: 给一个5x5的矩阵,求由多少个由连续6个元素组成的不一样的字符的个数。 解析: dfs + set去重搞定。 代码: #include <iostream>#include <cstdio>#include <set>#include <cstdlib>#include <algorithm>#include <cstring>#include <cm

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

树(Tree)——《啊哈!算法》

树 什么是树?树是一种特殊的图,不包含回路的连通无向树。 正因为树有着“不包含回路”这个特点,所以树就被赋予了很多特性。 一棵树中的任意两个结点有且仅有唯一的一条路径连通。一棵树如果有n个结点,那么它一定恰好有n-1条边。在一棵树中加一条边将会构成一个回路。 一棵树有且只有一个根结点。 没有父结点的结点称为根结点(祖先)。没有子结点的结点称为叶结点。如果一个结点既不是根结点也不是叶

深度优先(DFS)和广度优先(BFS)——算法

深度优先 深度优先搜索算法(英语:Depth-First-Search,DFS)是一种用于遍历或搜索树或图的算法。 沿着树的深度遍历树的节点,尽可能深的搜索树的分支,当节点v的所在边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点。这一过程一直进行到已发现从源节点可达的所有节点为止。如果还存在未被发现的节点,则选择其中一个作为源节点并重复以上过程,整个进程反复进行直到所有节点都被访

226 Invert Binary Tree

//226 Invert Binary Tree//算法思路:主要使用递归算法public class Solution {public TreeNode invertTree(TreeNode root) {//1 出口 空节点if (root==null)return null;//2 递归 调用自己TreeNode left = root.left;TreeNode right = ro

nyoj99(并查集+欧拉路+dfs)

单词拼接 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 5 描述 给你一些单词,请你判断能否把它们首尾串起来串成一串。 前一个单词的结尾应该与下一个单词的道字母相同。 如 aloha dog arachnid gopher tiger rat   可以拼接成:aloha.arachnid.dog.gopher.rat.tiger 输入 第一行是一个整

【CF】E. Anya and Cubes(双向DFS)

根据题意的话每次递归分3种情况 一共最多25个数,时间复杂度为3^25,太大了 我们可以分2次求解第一次求一半的结果,也就是25/2 = 12,记录结果 之后利用剩余的一半求结果 s-结果 = 之前记录过的结果 就可以 时间复杂度降低为 3 ^ (n/2+1) 题目链接:http://codeforces.com/contest/525/problem/E #include<set

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt