1401D - Maximum Distributed Tree

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <cassert>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define forn(i, n)      for(int i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}
inline int inv(int x) {
    return qpow(x, mod - 2);
}
ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
/**********************************************************/
const int N = 1e5 + 5;

int n;
vector<vector<int>> g;
ll sz[N];
ll p[N];

int dfs(int cur, int fa)
{
    sz[cur] = 1;
    for (auto x : g[cur])
    {
        if (x != fa)
        {
            sz[cur] += dfs(x, cur);
        }
    }
    return sz[cur];
}

bool cmp(pair<ll,ll> a, pair<ll,ll> b)
{
    return a.first * a.second < b.first * b.second;
}

int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        g = vector<vector<int>>(n + 1);
        vector<pair<ll,ll>> v(n);
        rep(i, 1, n - 1)
        {
            int u, v;
            cin >> u >> v;
            g[u].push_back(v);
            g[v].push_back(u);
        }
        int m;
        cin >> m;
        rep(i, 1, m)
            cin >> p[i];
        sort(p + 1, p + m + 1);
        dfs(1, -1);
        int tot = 0;
        rep(i, 2, n)
        {
            v[++tot] = { sz[i],n - sz[i] };
        }
        sort(v.begin() + 1, v.end(), cmp);
        ll ans = 0;
        if (n - 1 >= m)
        {
            int d = n - 1 - m;
            rep(i, 1, d)
            {
                ans += v[i].first * v[i].second%mod;
            }
            rep(i, 1, m)
            {
                ans += (v[i + d].first * v[i + d].second % mod) * p[i] % mod;
            }
        }
        else
        {
            ll tmp = 1;
            rep(i, n-1, m)
                tmp *= p[i], tmp%= mod;
            ans += tmp * (v[n-1].first * v[n-1].second % mod) % mod;
            rep(i, 1, n-2)
            {
                ans += (v[i].first * v[i].second % mod) * p[i] % mod;
            }
        }
        cout << ans%mod << endl;
    }
    return 0;
}

 

1401D - Maximum Distributed Tree

posted @ 2020-09-11 11:11  DeaL57  阅读(140)  评论(0编辑  收藏  举报