90012024-02-11 16:39:13xxxÚtadócpp17Wrong answer 8/50165ms32108 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct adat {
    int a, b, c, d, e;
};
const int MOD = 32609;
const int mxN = 1e5+1;
vector<vector<int> > adj;
vector<pair<int, int> > suly(mxN);
vector<adat> ans;
int n;

bool rendez(adat x, adat y) {
    if (x.e > y.e) {
        return true;
    }
    return false;
}

int dfs(int v, int p) {
    suly[v].first = 0;
    suly[v].second = 0;
    int cnt = 0;
    for(int u : adj[v]) {
        if (u != p) {
            if (cnt == 0) {
                suly[v].first = dfs(u, v);
                cnt++;
            } else if (cnt == 1) {
                suly[v].second = dfs(u, v);
                cnt++;
            } 
        }
    }
    return suly[v].first + suly[v].second + 1; 
}

void dfs2(int v, int p, int d) {
    int cnt = 0;
    for(int u : adj[v]) {
        if(u != p) {
            dfs2(u, v, cnt);
            cnt++;
        }
    }
    if (d == 0) {
        ans.push_back({suly[p].first, n-suly[p].first, p, v, 0});
    } else if (d == 1) {
        ans.push_back({n-suly[p].second, suly[p].second, p, v, 0});
    }
} 


signed main() {
    ios::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);
    cin >> n;
    adj.resize(n+1);
    for(int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    dfs(1, -1);
    dfs2(1, 0, -1);


    vector<int> v(n);
    for(int i = 0; i < n; i++) {
        cin >> v[i];
    }
    sort(v.rbegin(), v.rend());

    for(auto& [x, y, z, cs, w] : ans) {
        w = x * y;
        w %= MOD;
    }

    sort(ans.begin(), ans.end(), rendez);

    int anscnt = 0;

    for(int i = 0; i < n-1; i++) {
        anscnt += ans[i].e * v[i];
        anscnt %= MOD;
    }

    cout << anscnt * 2 << endl;

    for(int i = 0; i < n-1; i++) {
        cout << ans[i].c << ' ' << ans[i].d << ' ' << v[i] << endl;
    }
    
    

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base8/50
1Accepted0/04ms4788 KiB
2Wrong answer0/045ms10908 KiB
3Partially correct1/24ms5460 KiB
4Partially correct1/24ms5468 KiB
5Accepted2/24ms5724 KiB
6Accepted2/24ms5772 KiB
7Accepted2/24ms6028 KiB
8Wrong answer0/8165ms28980 KiB
9Wrong answer0/27ms7704 KiB
10Wrong answer0/27ms7860 KiB
11Wrong answer0/26ms8120 KiB
12Wrong answer0/27ms8240 KiB
13Wrong answer0/27ms8688 KiB
14Wrong answer0/2155ms20880 KiB
15Wrong answer0/2138ms22048 KiB
16Wrong answer0/2165ms23316 KiB
17Wrong answer0/2111ms24324 KiB
18Wrong answer0/2108ms25484 KiB
19Wrong answer0/2153ms26744 KiB
20Wrong answer0/2119ms27752 KiB
21Wrong answer0/2120ms28904 KiB
22Wrong answer0/2118ms29940 KiB
23Wrong answer0/2112ms31060 KiB
24Wrong answer0/2148ms32108 KiB