9232022-01-28 13:17:18Valaki2Útadócpp14Accepted 50/5086ms26352 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second

const int mod = 32609;

// tulkomplikalt adatszerkezet :( azt hittem kell... :)

struct edge {
    int to, subtree_size;
    edge() : to(0), subtree_size(0) {}
    edge(int to_, int subtree_size_) : to(to_), subtree_size(subtree_size_) {}
};

struct node {
    vector<edge> neighbours;
    node() {}
};

struct road {
    int product, a, b;
    bool operator < (const road & other) const {
        return product < other.product;
    }
    road(int product_, int a_, int b_) : product(product_), a(a_), b(b_) {}
};

int n;
vector<node> graph;
vector<int> weights;
vector<road> roads;

int dfs(int cur, int par) {
    int cur_subtree_size = 1;
    for(edge &nei : graph[cur].neighbours) {
        if(nei.to == par) continue;
        nei.subtree_size = dfs(nei.to, cur);
        cur_subtree_size += nei.subtree_size;
        roads.pb(road((n - nei.subtree_size) * nei.subtree_size, cur, nei.to));
    }
    return cur_subtree_size;
}

void solve() {
    cin >> n;
    weights.assign(n - 1, 0);
    graph.assign(1 + n, node());
    for(int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].neighbours.pb(edge(b, 0));
        graph[b].neighbours.pb(edge(a, 0));
    }
    for(int &x : weights) cin >> x;
    sort(weights.begin(), weights.end());
    dfs(1, 0);
    sort(roads.begin(), roads.end());
    int answer = 0;
    for(int i = 0; i < n - 1; i++) {
        answer += roads[i].product * weights[i];
        answer %= mod;
    }
    answer = (answer * 2) % mod;
    cout << answer << "\n";
    for(int i = 0; i < n - 1; i++) {
        cout << roads[i].a << " " << roads[i].b << " " << weights[i] << "\n";
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/02ms1752 KiB
2Accepted0/021ms7524 KiB
3Accepted2/21ms2288 KiB
4Accepted2/22ms2288 KiB
5Accepted2/22ms2292 KiB
6Accepted2/22ms2304 KiB
7Accepted2/21ms2304 KiB
8Accepted8/852ms23688 KiB
9Accepted2/22ms3620 KiB
10Accepted2/23ms3636 KiB
11Accepted2/22ms3652 KiB
12Accepted2/22ms3676 KiB
13Accepted2/23ms3696 KiB
14Accepted2/261ms16672 KiB
15Accepted2/257ms17688 KiB
16Accepted2/257ms18676 KiB
17Accepted2/278ms19764 KiB
18Accepted2/257ms20788 KiB
19Accepted2/257ms21820 KiB
20Accepted2/286ms22276 KiB
21Accepted2/261ms23208 KiB
22Accepted2/265ms24260 KiB
23Accepted2/259ms25312 KiB
24Accepted2/268ms26352 KiB