93852024-02-21 11:36:31szilÚtadócpp17Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/04ms4292 KiB
2Elfogadva0/024ms8492 KiB
3Elfogadva2/24ms4764 KiB
4Elfogadva2/24ms4960 KiB
5Elfogadva2/24ms5264 KiB
6Elfogadva2/24ms5384 KiB
7Elfogadva2/24ms5464 KiB
8Elfogadva8/854ms20584 KiB
9Elfogadva2/24ms5840 KiB
10Elfogadva2/24ms5844 KiB
11Elfogadva2/24ms6028 KiB
12Elfogadva2/24ms6220 KiB
13Elfogadva2/24ms6292 KiB
14Elfogadva2/254ms14684 KiB
15Elfogadva2/254ms14688 KiB
16Elfogadva2/252ms14688 KiB
17Elfogadva2/252ms14684 KiB
18Elfogadva2/254ms14688 KiB
19Elfogadva2/254ms14684 KiB
20Elfogadva2/257ms14716 KiB
21Elfogadva2/254ms14684 KiB
22Elfogadva2/256ms14836 KiB
23Elfogadva2/254ms14964 KiB
24Elfogadva2/256ms15052 KiB