45402023-03-29 13:43:57semmiÚtadócpp17Wrong answer 0/50115ms16248 KiB
#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> csucs(50001);
vector<long long> adok(50001), visited(50001, 0);
priority_queue<pair<long long, pair<long long, long long>>> ansok;
vector<int> osszeg(50001, 0);
int osszegmost = 0;

void dfs(long long st)
{
    visited[st] = 1;
    if (csucs[st].size() == 1)
    {
        osszeg[st] = 1;
        return;
    }
    long long sum = 0;

    for (int i : csucs[st])
    {
        if (!visited[i])
        {
            dfs(i);
            ansok.push({osszeg[i], {st, i}});
            sum += osszeg[i];
        }
    }

    osszeg[st] = sum;
}

int main()
{

    int n;
    cin >> n;

    for (int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        csucs[a].push_back(b);
        csucs[b].push_back(a);
    }

    for (int i = 0; i < n - 1; i++) {
        cin >> adok[i];
    }
    
    sort(adok.begin(), adok.begin()+n-1);

    dfs(1);
    osszeg[1]++;

    vector<pair<pair<int, int>, int>> ans;

    for (int i = n - 2; i >= 0; i--)
    {
        auto x = ansok.top();
        ansok.pop();
        ans.push_back({{x.second.first, x.second.second}, adok[i]});
        osszegmost += x.first * adok[i] * 2 % 32609;
    }
    cout << osszegmost << '\n';
    for (int i = 0; i < n - 1; i++)
    {
        cout << ans[i].first.first << ' ' << ans[i].first.second << ' ' << ans[i].second << '\n';
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base0/50
1Wrong answer0/04ms5916 KiB
2Runtime error0/028ms7692 KiB
3Wrong answer0/24ms6328 KiB
4Wrong answer0/24ms6536 KiB
5Wrong answer0/24ms6756 KiB
6Runtime error0/24ms7304 KiB
7Runtime error0/24ms7652 KiB
8Runtime error0/867ms10644 KiB
9Wrong answer0/26ms7828 KiB
10Wrong answer0/26ms7712 KiB
11Wrong answer0/26ms7712 KiB
12Runtime error0/24ms7956 KiB
13Runtime error0/24ms8028 KiB
14Wrong answer0/2107ms15996 KiB
15Wrong answer0/2105ms15996 KiB
16Wrong answer0/2111ms16004 KiB
17Wrong answer0/2109ms16248 KiB
18Wrong answer0/2115ms16212 KiB
19Wrong answer0/2107ms16208 KiB
20Runtime error0/270ms11264 KiB
21Runtime error0/268ms11440 KiB
22Runtime error0/270ms11688 KiB
23Runtime error0/270ms11940 KiB
24Runtime error0/270ms11940 KiB