251082026-02-17 23:00:45szabelrÚtadócpp17Elfogadva 50/50126ms9652 KiB
// Útadó jo.cpp : his file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int dfs(int v, int parent, 
    vector<vector<int>>& adj,
    vector<int> &under)
{
    under[v] = 1;
    for (auto to : adj[v])
    {
        if (to == parent)
            continue;
        under[v]+=dfs(to, v, adj,under);
    }
    return under[v];
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n; 
    cin >> n;
    long long sum = 0;
    vector<vector<int>> adj(n + 1);
    //2. edge id
    vector<pair<int,int>> edge;
    vector<int> under(n+1);
    for (int i = 1; i <= n-1; i++)
    {
        int a, b;
        cin >> a >> b;
        edge.push_back({ a,b });
        adj[a].push_back(b);
    }
    vector<int> weight(n - 1);
    for (int i = 0; i < n - 1; i++)
    {
        cin >> weight[i];
    }
    sort(weight.begin(), weight.end());

    dfs(1, -1, adj, under);
    vector<pair<long long,long long>> count(n - 1);
    for (int i = 0; i < n-1; i++)
    {
        count[i] = { under[edge[i].second] * (n - under[edge[i].second]) * 2 ,i} ;
    }
    sort(count.begin(), count.end());
    for (int i = 0; i < n - 1; i++)
    {
        sum += (count[i].first * weight[i]) % 32609;
    }
    cout << sum%32609 << endl;
    for (int i = 0; i < n - 1; i++)
    {
        int x = count[i].second;
        cout << edge[x].first << " " << edge[x].second << " " << weight[i] << endl;
    }
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms316 KiB
2Elfogadva0/046ms2180 KiB
3Elfogadva2/21ms500 KiB
4Elfogadva2/21ms508 KiB
5Elfogadva2/21ms316 KiB
6Elfogadva2/21ms316 KiB
7Elfogadva2/21ms316 KiB
8Elfogadva8/8119ms9652 KiB
9Elfogadva2/23ms316 KiB
10Elfogadva2/23ms316 KiB
11Elfogadva2/23ms508 KiB
12Elfogadva2/23ms500 KiB
13Elfogadva2/23ms500 KiB
14Elfogadva2/2116ms5000 KiB
15Elfogadva2/2112ms5036 KiB
16Elfogadva2/2111ms5040 KiB
17Elfogadva2/2109ms5036 KiB
18Elfogadva2/2123ms5036 KiB
19Elfogadva2/2123ms5036 KiB
20Elfogadva2/2112ms5036 KiB
21Elfogadva2/2112ms5308 KiB
22Elfogadva2/2123ms5164 KiB
23Elfogadva2/2126ms5240 KiB
24Elfogadva2/2112ms5292 KiB