95292024-02-22 15:43:13Vkrisztian01Útadócpp17Accepted 50/50104ms24528 KiB
#include <iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<queue>

using namespace std;

int const d=32609;

long long int n,a,b;
long long int s=0;
vector<long long int>edges;
vector<vector<pair<int,int> > > g;
vector<long long int>labels;
vector<long long int>dp;
priority_queue<pair<long long int,int> > seged;
vector<pair<int,int> > lo;

void dfs(int node)
{
    for(auto to:g[node])
    {
        dfs(to.first);
        dp[node]+=dp[to.first];
        seged.push(make_pair(dp[to.first]*(n-dp[to.first])*2,to.second));
    }
}

int main()
{
    cin>>n;
    g.resize(n+1);
    labels.resize(n-1);
    edges.resize(n);
    dp.assign(n+1,1);
    lo.resize(n+1);
    for(int i=1;i<n;i++)
    {
        cin>>a>>b;
        g[a].push_back(make_pair(b,i));
        lo[i]=make_pair(b,a);
    }
    for(int i=0;i<n-1;i++) cin>>labels[i];
    sort(labels.begin(),labels.end(),greater<>());
    dfs(1);
    a=0;
    while(!seged.empty())
    {
        s+=labels[a]*seged.top().first;
        s%=d;
        edges[seged.top().second]=labels[a];
        ++a;
        seged.pop();
    }
    cout<<s<<endl;
    for(int i=1;i<=n-1;i++)
    {
        cout<<lo[i].first<<" "<<lo[i].second<<" "<<edges[i]<<"\n";
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1876 KiB
2Accepted0/039ms6464 KiB
3Accepted2/23ms2292 KiB
4Accepted2/23ms2504 KiB
5Accepted2/23ms2584 KiB
6Accepted2/23ms2720 KiB
7Accepted2/23ms2932 KiB
8Accepted8/896ms24528 KiB
9Accepted2/24ms3360 KiB
10Accepted2/24ms3372 KiB
11Accepted2/24ms3364 KiB
12Accepted2/24ms3516 KiB
13Accepted2/24ms3476 KiB
14Accepted2/2100ms12760 KiB
15Accepted2/2104ms12720 KiB
16Accepted2/297ms12756 KiB
17Accepted2/298ms12812 KiB
18Accepted2/297ms12848 KiB
19Accepted2/2100ms13052 KiB
20Accepted2/2100ms13128 KiB
21Accepted2/2100ms13132 KiB
22Accepted2/2100ms13144 KiB
23Accepted2/2100ms13264 KiB
24Accepted2/2101ms13408 KiB