106062024-04-06 14:36:06MagyarKendeSZLGLogisztikai központcpp17Partially correct 39/50335ms53300 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 << "\n";
}
SubtaskSumTestVerdictTimeMemory
base39/50
1Accepted0/03ms2104 KiB
2Accepted0/065ms20368 KiB
3Accepted4/43ms3800 KiB
4Partially correct2/43ms3920 KiB
5Accepted4/42ms3816 KiB
6Partially correct2/43ms3924 KiB
7Partially correct2/43ms4148 KiB
8Accepted5/53ms4592 KiB
9Accepted2/274ms24988 KiB
10Accepted2/272ms26636 KiB
11Partially correct1/23ms7924 KiB
12Accepted2/2239ms8348 KiB
13Accepted2/26ms8996 KiB
14Accepted2/28ms10256 KiB
15Accepted2/267ms27364 KiB
16Partially correct1/264ms27896 KiB
17Accepted2/272ms31236 KiB
18Partially correct1/254ms27968 KiB
19Accepted2/264ms35324 KiB
20Partially correct1/3335ms53300 KiB