2422021-03-05 15:41:18balintttÚtadócpp14Accepted 50/50115ms23692 KiB
#include<bits/stdc++.h>

using namespace std;

class pont{
public:
    vector<int> hova;
    long long meret=0;
    int dfs();
};

class edge{
public:
    int a, b;
    long long meret=0;
    int resi;
};

vector<pont> p;
vector<edge> e;

int n;

int pont::dfs(){
    if(!meret){
        meret = 1;
        for(int i : hova){
            meret+=p[i].dfs();
        }
        return meret;
    }
    return 0;
}

bool cmp1(const edge& e1, const edge& e2){
    return e1.meret < e2.meret;
}

int main(){
    cin >> n;
    //cout << n << "=N\n";
    p.resize(n);
    e.resize(n-1);
    //cout << n << "=N\n";
    for(int i=0;i<n-1;i++){
        cin >> e[i].a >> e[i].b;
        --e[i].a; --e[i].b;
        p[e[i].a].hova.push_back(e[i].b);
        p[e[i].b].hova.push_back(e[i].a);
    }
    //cout << n << "=N\n";
    p[0].dfs();
    //cout << n << "=N\n";
    for(int i=0;i<n-1;i++){
        e[i].meret = min(p[e[i].a].meret,p[e[i].b].meret)*
        (n-(min(p[e[i].a].meret,p[e[i].b].meret)));
    }
    //cout << n << "=N\n";
    vector<int> v(n-1);
    for(int i=0;i<n-1;i++){
        cin >> v[i];
    }
    sort(v.begin(), v.end());
    sort(e.begin(), e.end(), cmp1);
    long long er = 0;
    for(int i=0;i<n-1;i++){
        e[i].resi = v[i];
        er+=e[i].meret%32609*e[i].resi;
        er%=32609;
    }
    cout << er*2%32609 << '\n';
    for(int i=0;i<n-1;i++){
        cout << e[i].a+1 << ' ' << e[i].b+1 << ' ' << e[i].resi << '\n';
    }
return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/02ms1752 KiB
2Accepted0/039ms5680 KiB
3Accepted2/21ms2260 KiB
4Accepted2/21ms2264 KiB
5Accepted2/21ms2272 KiB
6Accepted2/21ms2280 KiB
7Accepted2/21ms2284 KiB
8Accepted8/8114ms16908 KiB
9Accepted2/24ms3412 KiB
10Accepted2/23ms3436 KiB
11Accepted2/23ms3452 KiB
12Accepted2/23ms3600 KiB
13Accepted2/23ms3492 KiB
14Accepted2/2104ms13288 KiB
15Accepted2/297ms14328 KiB
16Accepted2/297ms15380 KiB
17Accepted2/2115ms16448 KiB
18Accepted2/296ms17464 KiB
19Accepted2/2108ms18508 KiB
20Accepted2/2108ms19548 KiB
21Accepted2/2104ms20532 KiB
22Accepted2/2101ms21592 KiB
23Accepted2/2104ms22640 KiB
24Accepted2/2101ms23692 KiB