106062024-04-06 14:36:06MagyarKendeSZLGLogisztikai központcpp17Részben helyes 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";
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base39/50
1Elfogadva0/03ms2104 KiB
2Elfogadva0/065ms20368 KiB
3Elfogadva4/43ms3800 KiB
4Részben helyes2/43ms3920 KiB
5Elfogadva4/42ms3816 KiB
6Részben helyes2/43ms3924 KiB
7Részben helyes2/43ms4148 KiB
8Elfogadva5/53ms4592 KiB
9Elfogadva2/274ms24988 KiB
10Elfogadva2/272ms26636 KiB
11Részben helyes1/23ms7924 KiB
12Elfogadva2/2239ms8348 KiB
13Elfogadva2/26ms8996 KiB
14Elfogadva2/28ms10256 KiB
15Elfogadva2/267ms27364 KiB
16Részben helyes1/264ms27896 KiB
17Elfogadva2/272ms31236 KiB
18Részben helyes1/254ms27968 KiB
19Elfogadva2/264ms35324 KiB
20Részben helyes1/3335ms53300 KiB