90082024-02-11 17:29:19xxxÚtadócpp17Wrong answer 10/50157ms27624 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)%MOD << endl;

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

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base10/50
1Accepted0/04ms4788 KiB
2Wrong answer0/046ms10396 KiB
3Accepted2/24ms5192 KiB
4Accepted2/24ms5488 KiB
5Accepted2/24ms5360 KiB
6Accepted2/24ms5356 KiB
7Accepted2/24ms5632 KiB
8Wrong answer0/8127ms27624 KiB
9Wrong answer0/27ms6428 KiB
10Wrong answer0/26ms6460 KiB
11Wrong answer0/26ms6720 KiB
12Wrong answer0/26ms6588 KiB
13Wrong answer0/26ms6592 KiB
14Wrong answer0/2108ms18240 KiB
15Wrong answer0/2141ms18440 KiB
16Wrong answer0/2107ms18712 KiB
17Wrong answer0/2134ms18644 KiB
18Wrong answer0/2109ms18640 KiB
19Wrong answer0/2116ms18336 KiB
20Wrong answer0/2157ms18504 KiB
21Wrong answer0/2119ms18616 KiB
22Wrong answer0/2144ms18696 KiB
23Wrong answer0/2152ms18800 KiB
24Wrong answer0/2112ms18804 KiB