Submission #540625

# Submission time Handle Problem Language Result Execution time Memory
540625 2022-03-21T08:58:59 Z AlperenT Parkovi (COCI22_parkovi) C++17
110 / 110
1915 ms 44056 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;
const long long INF = 2e18 + 5;

int n, k, a, b, c;

long long l, r, m, park[N], nongood[N];

bool ispark[N];

vector<pair<int, long long>> tree[N];

void dfs(int v, int p, int pw, long long x){
    if(tree[v].size() == 1) nongood[v] = 0;

    for(auto e : tree[v]){
        if(e.first != p){
            dfs(e.first, v, e.second, x);

            park[v] = min(park[v], park[e.first] + e.second);
        }
    }

    for(auto e : tree[v]){
        if(e.first != p){
            if(nongood[e.first] + e.second + park[v] > x){
                nongood[v] = max(nongood[v], nongood[e.first] + e.second);
            }
        }
    }

    if(park[v] > x) nongood[v] = max(nongood[v], 0ll);

    if(nongood[v] + pw > x) ispark[v] = true, park[v] = 0, nongood[v] = -INF;
}

bool check(long long x){
    memset(ispark, 0, sizeof(ispark)), fill(park, park + N, INF), fill(nongood, nongood + N, -INF);

    dfs(1, 0, 0, x);

    if(park[1] > x || (nongood[1] + park[1] > x)) ispark[1] = true;

    return accumulate(ispark + 1, ispark + n + 1, 0) <= k;
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);

    cin >> n >> k;

    for(int i = 1; i <= n - 1; i++){
        cin >> a >> b >> c;

        tree[a].push_back({b, c});
        tree[b].push_back({a, c});
    }

    l = -1, r = INF;

    while(r - l > 1){
        m = l + (r - l) / 2;

        if(check(m)) r = m;
        else l = m;
    }

    check(r);

    vector<int> v;

    for(int i = 1; i <= n; i++) if(ispark[i]) v.push_back(i);

    for(int i = 1; i <= n; i++) if(!ispark[i] && v.size() < k) v.push_back(i);

    cout << r << "\n";

    for(auto i : v) cout << i << " ";
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:77:59: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   77 |     for(int i = 1; i <= n; i++) if(!ispark[i] && v.size() < k) v.push_back(i);
      |                                                  ~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8276 KB Output is correct
2 Correct 19 ms 8340 KB Output is correct
3 Correct 13 ms 8336 KB Output is correct
4 Correct 14 ms 8276 KB Output is correct
5 Correct 19 ms 8340 KB Output is correct
6 Correct 14 ms 8272 KB Output is correct
7 Correct 13 ms 8336 KB Output is correct
8 Correct 12 ms 8344 KB Output is correct
9 Correct 14 ms 8276 KB Output is correct
10 Correct 13 ms 8228 KB Output is correct
11 Correct 13 ms 8340 KB Output is correct
12 Correct 14 ms 8228 KB Output is correct
13 Correct 13 ms 8276 KB Output is correct
14 Correct 13 ms 8344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 571 ms 37672 KB Output is correct
2 Correct 588 ms 38548 KB Output is correct
3 Correct 508 ms 17552 KB Output is correct
4 Correct 1761 ms 17936 KB Output is correct
5 Correct 1719 ms 17628 KB Output is correct
6 Correct 1798 ms 17640 KB Output is correct
7 Correct 1663 ms 17716 KB Output is correct
8 Correct 1818 ms 18220 KB Output is correct
9 Correct 1730 ms 17956 KB Output is correct
10 Correct 1824 ms 18188 KB Output is correct
11 Correct 1246 ms 19784 KB Output is correct
12 Correct 1127 ms 24132 KB Output is correct
13 Correct 1259 ms 25688 KB Output is correct
14 Correct 1046 ms 22660 KB Output is correct
15 Correct 1116 ms 22092 KB Output is correct
16 Correct 1122 ms 23600 KB Output is correct
17 Correct 1099 ms 22776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 567 ms 39120 KB Output is correct
2 Correct 559 ms 38160 KB Output is correct
3 Correct 528 ms 36684 KB Output is correct
4 Correct 516 ms 36480 KB Output is correct
5 Correct 564 ms 39704 KB Output is correct
6 Correct 603 ms 39052 KB Output is correct
7 Correct 612 ms 40084 KB Output is correct
8 Correct 557 ms 39244 KB Output is correct
9 Correct 543 ms 38932 KB Output is correct
10 Correct 547 ms 38216 KB Output is correct
11 Correct 515 ms 37208 KB Output is correct
12 Correct 597 ms 40708 KB Output is correct
13 Correct 580 ms 40856 KB Output is correct
14 Correct 576 ms 39872 KB Output is correct
15 Correct 535 ms 38296 KB Output is correct
16 Correct 512 ms 37072 KB Output is correct
17 Correct 515 ms 36932 KB Output is correct
18 Correct 552 ms 38648 KB Output is correct
19 Correct 531 ms 38724 KB Output is correct
20 Correct 537 ms 39444 KB Output is correct
21 Correct 528 ms 38556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8276 KB Output is correct
2 Correct 19 ms 8340 KB Output is correct
3 Correct 13 ms 8336 KB Output is correct
4 Correct 14 ms 8276 KB Output is correct
5 Correct 19 ms 8340 KB Output is correct
6 Correct 14 ms 8272 KB Output is correct
7 Correct 13 ms 8336 KB Output is correct
8 Correct 12 ms 8344 KB Output is correct
9 Correct 14 ms 8276 KB Output is correct
10 Correct 13 ms 8228 KB Output is correct
11 Correct 13 ms 8340 KB Output is correct
12 Correct 14 ms 8228 KB Output is correct
13 Correct 13 ms 8276 KB Output is correct
14 Correct 13 ms 8344 KB Output is correct
15 Correct 571 ms 37672 KB Output is correct
16 Correct 588 ms 38548 KB Output is correct
17 Correct 508 ms 17552 KB Output is correct
18 Correct 1761 ms 17936 KB Output is correct
19 Correct 1719 ms 17628 KB Output is correct
20 Correct 1798 ms 17640 KB Output is correct
21 Correct 1663 ms 17716 KB Output is correct
22 Correct 1818 ms 18220 KB Output is correct
23 Correct 1730 ms 17956 KB Output is correct
24 Correct 1824 ms 18188 KB Output is correct
25 Correct 1246 ms 19784 KB Output is correct
26 Correct 1127 ms 24132 KB Output is correct
27 Correct 1259 ms 25688 KB Output is correct
28 Correct 1046 ms 22660 KB Output is correct
29 Correct 1116 ms 22092 KB Output is correct
30 Correct 1122 ms 23600 KB Output is correct
31 Correct 1099 ms 22776 KB Output is correct
32 Correct 567 ms 39120 KB Output is correct
33 Correct 559 ms 38160 KB Output is correct
34 Correct 528 ms 36684 KB Output is correct
35 Correct 516 ms 36480 KB Output is correct
36 Correct 564 ms 39704 KB Output is correct
37 Correct 603 ms 39052 KB Output is correct
38 Correct 612 ms 40084 KB Output is correct
39 Correct 557 ms 39244 KB Output is correct
40 Correct 543 ms 38932 KB Output is correct
41 Correct 547 ms 38216 KB Output is correct
42 Correct 515 ms 37208 KB Output is correct
43 Correct 597 ms 40708 KB Output is correct
44 Correct 580 ms 40856 KB Output is correct
45 Correct 576 ms 39872 KB Output is correct
46 Correct 535 ms 38296 KB Output is correct
47 Correct 512 ms 37072 KB Output is correct
48 Correct 515 ms 36932 KB Output is correct
49 Correct 552 ms 38648 KB Output is correct
50 Correct 531 ms 38724 KB Output is correct
51 Correct 537 ms 39444 KB Output is correct
52 Correct 528 ms 38556 KB Output is correct
53 Correct 1643 ms 21984 KB Output is correct
54 Correct 1675 ms 22400 KB Output is correct
55 Correct 1821 ms 23008 KB Output is correct
56 Correct 1754 ms 22776 KB Output is correct
57 Correct 1737 ms 23428 KB Output is correct
58 Correct 1620 ms 22108 KB Output is correct
59 Correct 1802 ms 24696 KB Output is correct
60 Correct 1790 ms 21580 KB Output is correct
61 Correct 1630 ms 20448 KB Output is correct
62 Correct 1712 ms 21312 KB Output is correct
63 Correct 1915 ms 21592 KB Output is correct
64 Correct 1777 ms 22780 KB Output is correct
65 Correct 1865 ms 21996 KB Output is correct
66 Correct 1706 ms 22428 KB Output is correct
67 Correct 1566 ms 21228 KB Output is correct
68 Correct 1782 ms 24244 KB Output is correct
69 Correct 561 ms 42680 KB Output is correct
70 Correct 527 ms 40284 KB Output is correct
71 Correct 598 ms 44056 KB Output is correct
72 Correct 498 ms 21268 KB Output is correct
73 Correct 481 ms 20480 KB Output is correct
74 Correct 471 ms 22192 KB Output is correct
75 Correct 1088 ms 24664 KB Output is correct
76 Correct 1176 ms 23968 KB Output is correct
77 Correct 1137 ms 23808 KB Output is correct
78 Correct 1120 ms 23668 KB Output is correct