56502023-08-31 22:54:13TomaSajtÚtadócpp17Accepted 50/5061ms25624 KiB
#include <bits/stdc++.h>
using namespace std;
const int mod = 32609;

vector<vector<int>> g;
vector<int> par, sub_size;

void dfs(int u) {
  sub_size[u] = 1;
  for (int v : g[u]) {
    if (v == par[u]) continue;
    par[v] = u;
    dfs(v);
    sub_size[u] += sub_size[v];
  }
}

int main() {
  cin.tie(0), ios::sync_with_stdio(0);

  int n;
  cin >> n;

  g.resize(n + 1);
  par.resize(n + 1);
  sub_size.resize(n + 1);

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1);

  vector<array<int, 3>> edges;
  for (int i = 2; i <= n; i++) {
    int ss = sub_size[i];
    edges.push_back({ss * (n - ss) * 2, i, par[i]});
  }
  sort(edges.begin(), edges.end());

  vector<int> costs(n - 1);
  for (int& c : costs) cin >> c;
  sort(costs.begin(), costs.end());

  int res = 0;
  for (int i = 0; i < n - 1; i++) {
    res += (edges[i][0] % mod) * (costs[i] % mod);
    res %= mod;
  }
  cout << res << '\n';
  for (int i = 0; i < n - 1; i++) {
    cout << edges[i][1] << ' ' << edges[i][2] << ' ' << costs[i] << '\n';
  }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1828 KiB
2Accepted0/025ms6024 KiB
3Accepted2/23ms2744 KiB
4Accepted2/23ms2956 KiB
5Accepted2/23ms3176 KiB
6Accepted2/23ms3392 KiB
7Accepted2/23ms3600 KiB
8Accepted8/859ms20492 KiB
9Accepted2/23ms5088 KiB
10Accepted2/24ms5180 KiB
11Accepted2/23ms5212 KiB
12Accepted2/23ms5248 KiB
13Accepted2/24ms5252 KiB
14Accepted2/257ms14344 KiB
15Accepted2/259ms15376 KiB
16Accepted2/261ms16648 KiB
17Accepted2/257ms17704 KiB
18Accepted2/259ms18876 KiB
19Accepted2/261ms20012 KiB
20Accepted2/261ms20952 KiB
21Accepted2/261ms21992 KiB
22Accepted2/259ms23184 KiB
23Accepted2/261ms24456 KiB
24Accepted2/259ms25624 KiB