251092026-02-17 23:04:28szabelrÚtadócpp17Elfogadva 50/5046ms4052 KiB
#include <bits/stdc++.h>
using namespace std;

static const long long MOD = 32609;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<array<int,2>> child(n+1, {0,0});
    vector<int> deg(n+1, 0);
    vector<int> parent(n+1, 0);

    vector<pair<int,int>> edge(n); // 1..n-1
    for (int i = 1; i <= n-1; i++) {
        int a,b;
        cin >> a >> b;           // a parent, b child
        edge[i] = {a,b};
        parent[b] = a;
        child[a][deg[a]++] = b;  // 0 or 1 (binary)
    }

    vector<long long> w(n);      // 1..n-1
    for (int i = 1; i <= n-1; i++) cin >> w[i];
    sort(w.begin()+1, w.begin()+n); // ascending (ok)

    // subtree sizes via reverse order (no recursion)
    vector<int> under(n+1, 1);
    for (int v = n; v >= 2; v--) {   // nodes 2..n have a parent
        under[parent[v]] += under[v];
    }

    vector<pair<long long,int>> coef(n); // 1..n-1: (coef, edgeIndex)
    for (int i = 1; i <= n-1; i++) {
        int b = edge[i].second;
        long long s = under[b];
        long long c = 2LL * s * (n - s);
        coef[i] = {c, i};
    }
    sort(coef.begin()+1, coef.begin()+n); // ascending

    long long ans = 0;
    for (int i = 1; i <= n-1; i++) {
        ans = (ans + (coef[i].first % MOD) * (w[i] % MOD)) % MOD;
    }

    cout << ans << "\n";
    for (int i = 1; i <= n-1; i++) {
        int ei = coef[i].second; // which edge gets w[i]
        cout << edge[ei].first << " " << edge[ei].second << " " << w[i] << "\n";
    }
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms316 KiB
2Elfogadva0/017ms1844 KiB
3Elfogadva2/21ms500 KiB
4Elfogadva2/21ms316 KiB
5Elfogadva2/21ms316 KiB
6Elfogadva2/21ms368 KiB
7Elfogadva2/22ms508 KiB
8Elfogadva8/846ms4052 KiB
9Elfogadva2/22ms316 KiB
10Elfogadva2/22ms392 KiB
11Elfogadva2/22ms316 KiB
12Elfogadva2/22ms316 KiB
13Elfogadva2/22ms508 KiB
14Elfogadva2/243ms3892 KiB
15Elfogadva2/245ms3892 KiB
16Elfogadva2/243ms4008 KiB
17Elfogadva2/245ms3892 KiB
18Elfogadva2/243ms3892 KiB
19Elfogadva2/246ms3892 KiB
20Elfogadva2/245ms3892 KiB
21Elfogadva2/246ms3892 KiB
22Elfogadva2/245ms3892 KiB
23Elfogadva2/245ms3892 KiB
24Elfogadva2/245ms3904 KiB