93852024-02-21 11:36:31szilÚtadócpp17Accepted 50/5057ms20584 KiB
#include <bits/stdc++.h>

using ll = long long;
using namespace std;

const int MAXN = 50'001;
const int MOD = 32609;

struct Edge {
    ll w, u, v;

    Edge(int a, int b, int c) : w(a), u(b), v(c) {}
};

vector<int> g[MAXN];
ll s[MAXN], p[MAXN];

void dfs(int u) {
    s[u] = 1;
    for (int v : g[u]) {
        if (p[u] != v) {
            p[v] = u;
            dfs(v);
            s[u] += s[v];
        }
    }
}

void solve() {
    int n; cin >> n;
    for (int i = 0; i < n-1; i++) {
        int u, v; cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    vector<ll> cost(n-1);
    for (ll &i : cost) cin >> i;
    dfs(1);
    vector<Edge> edges;
    for (int i = 2; i <= n; i++) {
        edges.emplace_back(s[i]*(n-s[i]), i, p[i]);
    }
    sort(cost.begin(), cost.end());
    sort(edges.begin(), edges.end(), [](const Edge &a, const Edge &b){
        return a.w < b.w;
    });
    ll ans = 0;
    for (int i = 0; i < n-1; i++) {
        ans += 2ll*edges[i].w*cost[i];
        ans %= MOD;
    }
    cout << ans << "\n";
    for (int i = 0; i < n-1; i++) {
        cout << edges[i].u << " " << edges[i].v << " " << cost[i] << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/04ms4292 KiB
2Accepted0/024ms8492 KiB
3Accepted2/24ms4764 KiB
4Accepted2/24ms4960 KiB
5Accepted2/24ms5264 KiB
6Accepted2/24ms5384 KiB
7Accepted2/24ms5464 KiB
8Accepted8/854ms20584 KiB
9Accepted2/24ms5840 KiB
10Accepted2/24ms5844 KiB
11Accepted2/24ms6028 KiB
12Accepted2/24ms6220 KiB
13Accepted2/24ms6292 KiB
14Accepted2/254ms14684 KiB
15Accepted2/254ms14688 KiB
16Accepted2/252ms14688 KiB
17Accepted2/252ms14684 KiB
18Accepted2/254ms14688 KiB
19Accepted2/254ms14684 KiB
20Accepted2/257ms14716 KiB
21Accepted2/254ms14684 KiB
22Accepted2/256ms14836 KiB
23Accepted2/254ms14964 KiB
24Accepted2/256ms15052 KiB