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;
}
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值