148252025-02-03 18:59:36csdavidÚtadócpp17Accepted 50/5054ms7524 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

const int mod=32609;

using namespace std;

struct csucs{
    vector<int> gyerek;
    int alatta=1, szulo, bejart=0, megvan=0;
};
struct ut{
    long long atjaras, p1, p2;
};
bool alma(ut x1, ut x2){
    return x1.atjaras<x2.atjaras;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, x, y;
    cin >> n;
    csucs a[n];
    a[0].szulo=-1;
    for(int i=1; i<n; i++){
        cin >> x >> y;
        x--;
        y--;
        a[x].gyerek.push_back(y);
        a[y].szulo=x;
    }
    queue<int> q;
    for(int i=0; i<n; i++){
        if(a[i].gyerek.size()==0){
            q.push(i);
            a[i].bejart=1;
        }
    }
    while(!q.empty()){
        x=q.front();
        q.pop();
        y=a[x].szulo;
        a[y].alatta+=a[x].alatta;
        a[y].megvan++;
        if(a[y].megvan==a[y].gyerek.size()){
            q.push(y);
        }
    }
    long long ar[n-1];
    ut b[n-1];

    for(int i=1; i<n; i++){
        b[i-1].atjaras=a[i].alatta*(n-a[i].alatta)*2;
        b[i-1].p1=i;
        b[i-1].p2=a[i].szulo;
        //cout << i+1 << ": " << b[i-1].atjaras << '\n';
        cin >> ar[i-1];
    }
    sort(b, b+n-1, alma);
    sort(ar, ar+n-1);
    long long profit=0;
    for(int i=0; i<n-1; i++){
        profit+=(ar[i]%mod)*(b[i].atjaras%mod);
        profit=profit%mod;
    }
    cout << profit << '\n';
    for(int i=0; i<n-1; i++){
        cout << b[i].p1+1 << ' ' << b[i].p2+1 << ' ' << ar[i] << '\n';
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/019ms2868 KiB
3Accepted2/21ms512 KiB
4Accepted2/21ms316 KiB
5Accepted2/21ms316 KiB
6Accepted2/21ms500 KiB
7Accepted2/21ms316 KiB
8Accepted8/854ms7524 KiB
9Accepted2/22ms316 KiB
10Accepted2/22ms316 KiB
11Accepted2/22ms316 KiB
12Accepted2/22ms316 KiB
13Accepted2/22ms316 KiB
14Accepted2/246ms6744 KiB
15Accepted2/248ms6820 KiB
16Accepted2/246ms6796 KiB
17Accepted2/248ms6964 KiB
18Accepted2/248ms6880 KiB
19Accepted2/248ms6964 KiB
20Accepted2/248ms6980 KiB
21Accepted2/248ms6964 KiB
22Accepted2/248ms6960 KiB
23Accepted2/248ms6964 KiB
24Accepted2/250ms6964 KiB