Submission #764967

# Submission time Handle Problem Language Result Execution time Memory
764967 2023-06-24T07:02:04 Z drdilyor Parkovi (COCI22_parkovi) C++17
110 / 110
1365 ms 40964 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int inf = 1e9;
constexpr ll infl = 1e18;

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<vector<pair<int,int>>> adj(n);
    for( int i = 0; i < n-1; i++) {
        int a, b, w;
        cin >> a >> b >> w;
        a--;b--;
        adj[a].emplace_back(b, w);
        adj[b].emplace_back(a, w);
    }

    int root = 0;
    for (; root < n; root++)
        if (adj[root].size() == 1) break;

    vector<int> built;
    auto check = [&](ll maxd)->bool{
        vector<pair<ll,ll>> memo(n, {-1, -1});
        built.assign(n, 0);
        int parks = 0;
        auto dfs = [&](auto& dfs, int i, int p=-1)->pair<ll,ll> {
            if (memo[i].first !=-1) return memo[i];
            ll close_build = infl;
            ll far = -infl;
            for (auto [e, w] : adj[i]) {
                if (e == p) continue;
                auto [c, f] = dfs(dfs, e, i);
                if (f + w > maxd) {
                    parks++;
                    built[e] = 1;
                    close_build = min(close_build, (ll)w);
                } else close_build = min(close_build, c + w);
            }
            for (auto [e, w] : adj[i]) {
                if (e == p) continue;
                if (built[e]) continue;
                auto [c, f] = dfs(dfs, e, i);
                if (f + w + close_build <= maxd) continue;
                far = max(far, f + w);
            }
            if (close_build > maxd) far = max(far, 0ll);
            //cout << "[maxd,i,close_build,far,parks]=" << maxd << ' ' << i << ',' << close_build << ' ' << far << ',' << parks << endl;
            return memo[i] = {close_build, far};
        };
        auto [c, f] = dfs(dfs, root);
        if (f >= 0) {
            built[root] = 1;
            parks++;
        }
        return parks <= k;
    };

    ll l = 0, r = infl;
    while (l < r-1) {
        ll mid = (l+r) / 2;
        if (check(mid)) r = mid;
        else l = mid;
    }
    check(r);
    cout << r << '\n';
    vector<int> parks;
    for (int i = 0; i < n; i++)
        if (built[i]) parks.push_back(i);
    for (int i = 0; i < n; i++)
        if ((int)parks.size() < k  && !built[i])
            parks.push_back(i);
    for (int i : parks) cout << i+1 << ' ';
    cout << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 232 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 454 ms 34756 KB Output is correct
2 Correct 463 ms 35720 KB Output is correct
3 Correct 443 ms 16604 KB Output is correct
4 Correct 1151 ms 15328 KB Output is correct
5 Correct 917 ms 14904 KB Output is correct
6 Correct 1039 ms 14912 KB Output is correct
7 Correct 897 ms 15036 KB Output is correct
8 Correct 1044 ms 15812 KB Output is correct
9 Correct 1033 ms 15360 KB Output is correct
10 Correct 1073 ms 15744 KB Output is correct
11 Correct 731 ms 17108 KB Output is correct
12 Correct 815 ms 16444 KB Output is correct
13 Correct 805 ms 18348 KB Output is correct
14 Correct 736 ms 16972 KB Output is correct
15 Correct 695 ms 17136 KB Output is correct
16 Correct 716 ms 17356 KB Output is correct
17 Correct 679 ms 16972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 522 ms 36308 KB Output is correct
2 Correct 523 ms 35344 KB Output is correct
3 Correct 444 ms 33588 KB Output is correct
4 Correct 424 ms 33416 KB Output is correct
5 Correct 490 ms 35888 KB Output is correct
6 Correct 464 ms 35680 KB Output is correct
7 Correct 483 ms 36892 KB Output is correct
8 Correct 465 ms 36556 KB Output is correct
9 Correct 481 ms 36220 KB Output is correct
10 Correct 460 ms 35540 KB Output is correct
11 Correct 489 ms 34256 KB Output is correct
12 Correct 502 ms 37132 KB Output is correct
13 Correct 547 ms 37496 KB Output is correct
14 Correct 497 ms 36652 KB Output is correct
15 Correct 467 ms 35524 KB Output is correct
16 Correct 430 ms 34052 KB Output is correct
17 Correct 438 ms 33940 KB Output is correct
18 Correct 448 ms 35772 KB Output is correct
19 Correct 431 ms 34868 KB Output is correct
20 Correct 482 ms 35812 KB Output is correct
21 Correct 470 ms 35044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 232 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 454 ms 34756 KB Output is correct
16 Correct 463 ms 35720 KB Output is correct
17 Correct 443 ms 16604 KB Output is correct
18 Correct 1151 ms 15328 KB Output is correct
19 Correct 917 ms 14904 KB Output is correct
20 Correct 1039 ms 14912 KB Output is correct
21 Correct 897 ms 15036 KB Output is correct
22 Correct 1044 ms 15812 KB Output is correct
23 Correct 1033 ms 15360 KB Output is correct
24 Correct 1073 ms 15744 KB Output is correct
25 Correct 731 ms 17108 KB Output is correct
26 Correct 815 ms 16444 KB Output is correct
27 Correct 805 ms 18348 KB Output is correct
28 Correct 736 ms 16972 KB Output is correct
29 Correct 695 ms 17136 KB Output is correct
30 Correct 716 ms 17356 KB Output is correct
31 Correct 679 ms 16972 KB Output is correct
32 Correct 522 ms 36308 KB Output is correct
33 Correct 523 ms 35344 KB Output is correct
34 Correct 444 ms 33588 KB Output is correct
35 Correct 424 ms 33416 KB Output is correct
36 Correct 490 ms 35888 KB Output is correct
37 Correct 464 ms 35680 KB Output is correct
38 Correct 483 ms 36892 KB Output is correct
39 Correct 465 ms 36556 KB Output is correct
40 Correct 481 ms 36220 KB Output is correct
41 Correct 460 ms 35540 KB Output is correct
42 Correct 489 ms 34256 KB Output is correct
43 Correct 502 ms 37132 KB Output is correct
44 Correct 547 ms 37496 KB Output is correct
45 Correct 497 ms 36652 KB Output is correct
46 Correct 467 ms 35524 KB Output is correct
47 Correct 430 ms 34052 KB Output is correct
48 Correct 438 ms 33940 KB Output is correct
49 Correct 448 ms 35772 KB Output is correct
50 Correct 431 ms 34868 KB Output is correct
51 Correct 482 ms 35812 KB Output is correct
52 Correct 470 ms 35044 KB Output is correct
53 Correct 1049 ms 19304 KB Output is correct
54 Correct 1365 ms 19908 KB Output is correct
55 Correct 1085 ms 20768 KB Output is correct
56 Correct 1339 ms 20248 KB Output is correct
57 Correct 1227 ms 20532 KB Output is correct
58 Correct 1199 ms 19444 KB Output is correct
59 Correct 1014 ms 21464 KB Output is correct
60 Correct 1178 ms 19276 KB Output is correct
61 Correct 1004 ms 17600 KB Output is correct
62 Correct 949 ms 18028 KB Output is correct
63 Correct 1248 ms 19356 KB Output is correct
64 Correct 1087 ms 19308 KB Output is correct
65 Correct 1261 ms 19556 KB Output is correct
66 Correct 1143 ms 19324 KB Output is correct
67 Correct 899 ms 18424 KB Output is correct
68 Correct 1301 ms 20944 KB Output is correct
69 Correct 480 ms 39900 KB Output is correct
70 Correct 443 ms 37208 KB Output is correct
71 Correct 549 ms 40964 KB Output is correct
72 Correct 376 ms 19920 KB Output is correct
73 Correct 430 ms 19420 KB Output is correct
74 Correct 514 ms 20172 KB Output is correct
75 Correct 1054 ms 21780 KB Output is correct
76 Correct 1072 ms 21840 KB Output is correct
77 Correct 792 ms 20912 KB Output is correct
78 Correct 879 ms 20416 KB Output is correct