46652023-03-30 18:37:33ZsofiaKeresztelyÚtadócpp14Accepted 50/50104ms19228 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr ll MOD = 32609;
#define fi first
#define se second
vector<vector<int> > g;
vector<int> p;
vector<pair<ll, int> > times;
ll n;

ll mul(ll a, ll b){
    a %= MOD;
    b %= MOD;
    return a * b % MOD;
}

int dfs(int v){
    ll r = 1;
    for (int x : g[v]){
        if (x == p[v]) continue;
        p[x] = v;
        r += dfs(x);
    }
    times[v].se = v;
    times[v].fi = r * 2 * (n - r);
    return r;
}

int main()
{
    cin >> n;
    g.resize(n+1);
    p.resize(n+1);
    times.resize(n+1);
    for (int i=0; i<n-1; i++){
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    p[1] = 0;
    dfs(1);
    vector<ll> w(n+1, 0);
    for (int i=2; i<=n; i++){
        cin >> w[i];
    }
    sort(w.begin(), w.end());
    sort(times.begin(), times.end());
    ll op = 0;
    for (int i=2; i<=n; i++){
        op += mul(w[i], times[i].fi);
        op %= MOD;
    }
    cout << op << "\n";
    for (int i=2; i<=n; i++){
        cout << times[i].se << " " << p[times[i].se] << " " << w[i] << "\n";
    }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1816 KiB
2Accepted0/039ms5192 KiB
3Accepted2/23ms2228 KiB
4Accepted2/23ms2340 KiB
5Accepted2/23ms2536 KiB
6Accepted2/23ms2748 KiB
7Accepted2/23ms2964 KiB
8Accepted8/897ms19228 KiB
9Accepted2/24ms3448 KiB
10Accepted2/24ms3660 KiB
11Accepted2/24ms3712 KiB
12Accepted2/24ms3864 KiB
13Accepted2/24ms3820 KiB
14Accepted2/297ms12100 KiB
15Accepted2/297ms12060 KiB
16Accepted2/297ms12064 KiB
17Accepted2/297ms12316 KiB
18Accepted2/297ms12272 KiB
19Accepted2/296ms12272 KiB
20Accepted2/2100ms12340 KiB
21Accepted2/297ms12344 KiB
22Accepted2/297ms12344 KiB
23Accepted2/297ms12556 KiB
24Accepted2/2104ms12544 KiB