46652023-03-30 18:37:33ZsofiaKeresztelyÚtadócpp14Elfogadva 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";
    }
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/03ms1816 KiB
2Elfogadva0/039ms5192 KiB
3Elfogadva2/23ms2228 KiB
4Elfogadva2/23ms2340 KiB
5Elfogadva2/23ms2536 KiB
6Elfogadva2/23ms2748 KiB
7Elfogadva2/23ms2964 KiB
8Elfogadva8/897ms19228 KiB
9Elfogadva2/24ms3448 KiB
10Elfogadva2/24ms3660 KiB
11Elfogadva2/24ms3712 KiB
12Elfogadva2/24ms3864 KiB
13Elfogadva2/24ms3820 KiB
14Elfogadva2/297ms12100 KiB
15Elfogadva2/297ms12060 KiB
16Elfogadva2/297ms12064 KiB
17Elfogadva2/297ms12316 KiB
18Elfogadva2/297ms12272 KiB
19Elfogadva2/296ms12272 KiB
20Elfogadva2/2100ms12340 KiB
21Elfogadva2/297ms12344 KiB
22Elfogadva2/297ms12344 KiB
23Elfogadva2/297ms12556 KiB
24Elfogadva2/2104ms12544 KiB