106072024-04-06 14:37:18MagyarKendeSZLGLogisztikai központcpp17Részben helyes 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";
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base39/50
1Elfogadva0/03ms1824 KiB
2Elfogadva0/068ms18856 KiB
3Elfogadva4/43ms2536 KiB
4Részben helyes2/43ms2652 KiB
5Elfogadva4/43ms2676 KiB
6Részben helyes2/43ms2896 KiB
7Részben helyes2/43ms2980 KiB
8Elfogadva5/53ms3128 KiB
9Elfogadva2/272ms21768 KiB
10Elfogadva2/279ms21860 KiB
11Részben helyes1/23ms3396 KiB
12Elfogadva2/2263ms3752 KiB
13Elfogadva2/26ms4408 KiB
14Elfogadva2/28ms5260 KiB
15Elfogadva2/264ms20580 KiB
16Részben helyes1/264ms19656 KiB
17Elfogadva2/272ms21364 KiB
18Részben helyes1/252ms16664 KiB
19Elfogadva2/261ms21400 KiB
20Részben helyes1/3351ms38128 KiB