9232022-01-28 13:17:18Valaki2Útadócpp14Elfogadva 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/02ms1752 KiB
2Elfogadva0/021ms7524 KiB
3Elfogadva2/21ms2288 KiB
4Elfogadva2/22ms2288 KiB
5Elfogadva2/22ms2292 KiB
6Elfogadva2/22ms2304 KiB
7Elfogadva2/21ms2304 KiB
8Elfogadva8/852ms23688 KiB
9Elfogadva2/22ms3620 KiB
10Elfogadva2/23ms3636 KiB
11Elfogadva2/22ms3652 KiB
12Elfogadva2/22ms3676 KiB
13Elfogadva2/23ms3696 KiB
14Elfogadva2/261ms16672 KiB
15Elfogadva2/257ms17688 KiB
16Elfogadva2/257ms18676 KiB
17Elfogadva2/278ms19764 KiB
18Elfogadva2/257ms20788 KiB
19Elfogadva2/257ms21820 KiB
20Elfogadva2/286ms22276 KiB
21Elfogadva2/261ms23208 KiB
22Elfogadva2/265ms24260 KiB
23Elfogadva2/259ms25312 KiB
24Elfogadva2/268ms26352 KiB