106072024-04-06 14:37:18MagyarKendeSZLGLogisztikai központcpp17Partially correct 39/50351ms38128 KiB
#include <bits/stdc++.h>

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define size(v) (int)v.size()
#define has(s, e) s.count(e)

using namespace std;
using ll = long long;

int N;
vector<vector<pair<int, ll>>> g;
vector<ll> distS, par_distS;
vector<int> parS;

ll result_t = 1e18;
set<int> result_nodeS;

void dfs(int u, int par) {
    for (auto [v, w] : g[u]) {
        if (v == par) continue;
        if (distS[v] < distS[u] + w) {
            distS[v] = distS[u] + w;
            parS[v] = u;
            par_distS[v] = w;
        }
        dfs(v, u);
    }
}

void proc(int end) {
    vector<pair<int, ll>> path;
    int curr = end;

    do {
        path.push_back({curr, par_distS[curr]});
        curr = parS[curr];
    } while (curr);

    ll sum = 0;
    for (auto [u, w] : path) sum += w;

    ll running_sum = 0;
    for (int i = 0; i < size(path); i++) {
        ll curr = max(running_sum, sum - running_sum);

        if (curr < result_t) {
            result_t = curr;
            result_nodeS.clear();
            result_nodeS.insert(path[i].first);
        }
        else if (curr == result_t) {
            result_nodeS.insert(path[i].first);
        }

        running_sum += path[i].second;
    }
}

void get_ends(int start) {
    distS    .assign(N + 1, 0);
    parS     .assign(N + 1, 0);
    par_distS.assign(N + 1, 0);

    dfs(start, 0);
    int end = max_element(all(distS)) - distS.begin();

    for (int u = 1; u <= N; u++) {
        if (distS[u] == distS[end]) {
            proc(u);
        }
    }
}

int main() {
    cin.tie(0); ios::sync_with_stdio(0);
    cin >> N;

    if (N == 1) {
        cout << "0\n1\n1";
        exit(0);
    }

    g        .resize(N + 1);
    distS    .resize(N + 1);
    parS     .resize(N + 1);
    par_distS.resize(N + 1);

    for (int i = 1; i < N; i++) {
        int U, V, W;
        cin >> U >> V >> W;
        g[U].push_back({V, W});
        g[V].push_back({U, W});
    }

    dfs(1, 0);
    int start = max_element(all(distS)) - distS.begin();

    for (int u = 1; u <= N; u++) {
        if (distS[u] == distS[start]) {
            get_ends(u);
        }
    }

    cout << result_t << "\n" << size(result_nodeS) << "\n";
    for (int u : result_nodeS) cout << u << " ";
    cout << "\n";
}
SubtaskSumTestVerdictTimeMemory
base39/50
1Accepted0/03ms1824 KiB
2Accepted0/068ms18856 KiB
3Accepted4/43ms2536 KiB
4Partially correct2/43ms2652 KiB
5Accepted4/43ms2676 KiB
6Partially correct2/43ms2896 KiB
7Partially correct2/43ms2980 KiB
8Accepted5/53ms3128 KiB
9Accepted2/272ms21768 KiB
10Accepted2/279ms21860 KiB
11Partially correct1/23ms3396 KiB
12Accepted2/2263ms3752 KiB
13Accepted2/26ms4408 KiB
14Accepted2/28ms5260 KiB
15Accepted2/264ms20580 KiB
16Partially correct1/264ms19656 KiB
17Accepted2/272ms21364 KiB
18Partially correct1/252ms16664 KiB
19Accepted2/261ms21400 KiB
20Partially correct1/3351ms38128 KiB