Submission #765000

# Submission time Handle Problem Language Result Execution time Memory
765000 2023-06-24T07:19:58 Z dxz05 Parkovi (COCI22_parkovi) C++17
50 / 110
3000 ms 78788 KB
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
#define BIT(x, i) (((x) >> (i)) & 1)
//#define endl '\n'

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 2e5 + 30;

int n;
vector<pair<int, int>> g[N];

ll dep[N];
int up[N][18];
ll sum[N][18];

void dfs0(int v, int p){
    up[v][0] = p;
    sum[v][0] = dep[v] - dep[p];
    for (int i = 1; i < 18; i++){
        int pp = up[v][i - 1];
        up[v][i] = up[pp][i - 1];
        sum[v][i] = sum[v][i - 1] + sum[pp][i - 1];
    }

    for (auto [u, w] : g[v]){
        if (u != p){
            dep[u] = dep[v] + w;
            dfs0(u, v);
        }
    }
}

bool deleted[N];

void dfs(int v, int p, ll dist){
    deleted[v] = true;

    for (auto [u, w] : g[v]){
        if (u == p) continue;
        if (w <= dist){
            dfs(u, v, dist - w);
        } else {
            break;
        }
    }
}

int parks[N];

int parks_cnt(ll d){
    priority_queue<pair<ll, int>> pq;
    for (int i = 1; i <= n; i++) pq.emplace(dep[i], i);
    fill(deleted + 1, deleted + n + 1, false);

    int sz = 0;
    while (!pq.empty()){
        int v = pq.top().second;
        if (deleted[v]){
            pq.pop();
            continue;
        }

        ll x = d;
        for (int i = 17; i >= 0; i--){
            if (sum[v][i] <= x){
                x -= sum[v][i];
                v = up[v][i];
            }
        }

        parks[sz++] = v;

        dfs(v, -1, d);
    }

    return sz;
}

int ans[N];

void solve(){
    int k;
    cin >> n >> k;

    for (int i = 1; i < n; i++){
        int u, v, w;
        cin >> u >> v >> w;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }

    int root = 1;
    for (int i = 1; i <= n; i++){
        if (g[i].size() > 1) root = i;

        sort(all(g[i]), [&](auto x, auto y){
            return x.second < y.second;
        });

    }

    dfs0(root, root);

    int sz = 0;

    ll l = 0, r = 2e14;
    while (l <= r){
        ll m = (l + r) >> 1;

        int res = parks_cnt(m);

        if (res <= k){
            sz = res;
            for (int i = 0; i < res; i++) ans[i] = parks[i];
            r = m - 1;
        } else {
            l = m + 1;
        }
    }

    sort(ans, ans + sz);

    cout << ++r << endl;
    for (int i = 0; i < sz; i++) cout << ans[i] << ' ';

    int x = k - sz;
    for (int i = 1; i <= n; i++){
        if (x == 0) break;
        int j = lower_bound(ans, ans + sz, i) - ans;
        if (j == sz || ans[j] != i){
            cout << i << ' ';
            x--;
        }
    }

    cout << endl;
}

int main(){
    clock_t startTime = clock();
    ios_base::sync_with_stdio(false);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int test_cases = 1;
//    cin >> test_cases;

    for (int test = 1; test <= test_cases; test++){
        // cout << (solve() ? "YES" : "NO") << endl;
        solve();
    }

#ifdef LOCAL
    cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif

    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:153:13: warning: unused variable 'startTime' [-Wunused-variable]
  153 |     clock_t startTime = clock();
      |             ^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 4 ms 5024 KB Output is correct
4 Correct 3 ms 5020 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 5028 KB Output is correct
9 Correct 3 ms 5024 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5056 KB Output is correct
14 Correct 3 ms 4952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 937 ms 74060 KB Output is correct
2 Correct 933 ms 76072 KB Output is correct
3 Correct 1940 ms 64536 KB Output is correct
4 Correct 2099 ms 60632 KB Output is correct
5 Correct 2007 ms 59176 KB Output is correct
6 Correct 1944 ms 59256 KB Output is correct
7 Correct 2495 ms 59632 KB Output is correct
8 Correct 2525 ms 62308 KB Output is correct
9 Correct 2787 ms 60880 KB Output is correct
10 Correct 2556 ms 62036 KB Output is correct
11 Correct 1846 ms 63448 KB Output is correct
12 Correct 1838 ms 61280 KB Output is correct
13 Correct 1996 ms 66192 KB Output is correct
14 Correct 1766 ms 60948 KB Output is correct
15 Correct 1791 ms 61096 KB Output is correct
16 Correct 1844 ms 61912 KB Output is correct
17 Correct 1905 ms 61236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 882 ms 77012 KB Output is correct
2 Correct 866 ms 75552 KB Output is correct
3 Correct 873 ms 71764 KB Output is correct
4 Correct 898 ms 71960 KB Output is correct
5 Correct 927 ms 75432 KB Output is correct
6 Correct 1103 ms 75848 KB Output is correct
7 Correct 1079 ms 77808 KB Output is correct
8 Correct 913 ms 77720 KB Output is correct
9 Correct 863 ms 77108 KB Output is correct
10 Correct 886 ms 75920 KB Output is correct
11 Correct 903 ms 73280 KB Output is correct
12 Correct 1065 ms 77952 KB Output is correct
13 Correct 1119 ms 78788 KB Output is correct
14 Correct 1127 ms 77580 KB Output is correct
15 Correct 900 ms 75684 KB Output is correct
16 Correct 890 ms 72988 KB Output is correct
17 Correct 866 ms 72548 KB Output is correct
18 Correct 938 ms 76228 KB Output is correct
19 Correct 887 ms 73600 KB Output is correct
20 Correct 1002 ms 75696 KB Output is correct
21 Correct 1018 ms 74540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 4 ms 5024 KB Output is correct
4 Correct 3 ms 5020 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 5028 KB Output is correct
9 Correct 3 ms 5024 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 5056 KB Output is correct
14 Correct 3 ms 4952 KB Output is correct
15 Correct 937 ms 74060 KB Output is correct
16 Correct 933 ms 76072 KB Output is correct
17 Correct 1940 ms 64536 KB Output is correct
18 Correct 2099 ms 60632 KB Output is correct
19 Correct 2007 ms 59176 KB Output is correct
20 Correct 1944 ms 59256 KB Output is correct
21 Correct 2495 ms 59632 KB Output is correct
22 Correct 2525 ms 62308 KB Output is correct
23 Correct 2787 ms 60880 KB Output is correct
24 Correct 2556 ms 62036 KB Output is correct
25 Correct 1846 ms 63448 KB Output is correct
26 Correct 1838 ms 61280 KB Output is correct
27 Correct 1996 ms 66192 KB Output is correct
28 Correct 1766 ms 60948 KB Output is correct
29 Correct 1791 ms 61096 KB Output is correct
30 Correct 1844 ms 61912 KB Output is correct
31 Correct 1905 ms 61236 KB Output is correct
32 Correct 882 ms 77012 KB Output is correct
33 Correct 866 ms 75552 KB Output is correct
34 Correct 873 ms 71764 KB Output is correct
35 Correct 898 ms 71960 KB Output is correct
36 Correct 927 ms 75432 KB Output is correct
37 Correct 1103 ms 75848 KB Output is correct
38 Correct 1079 ms 77808 KB Output is correct
39 Correct 913 ms 77720 KB Output is correct
40 Correct 863 ms 77108 KB Output is correct
41 Correct 886 ms 75920 KB Output is correct
42 Correct 903 ms 73280 KB Output is correct
43 Correct 1065 ms 77952 KB Output is correct
44 Correct 1119 ms 78788 KB Output is correct
45 Correct 1127 ms 77580 KB Output is correct
46 Correct 900 ms 75684 KB Output is correct
47 Correct 890 ms 72988 KB Output is correct
48 Correct 866 ms 72548 KB Output is correct
49 Correct 938 ms 76228 KB Output is correct
50 Correct 887 ms 73600 KB Output is correct
51 Correct 1002 ms 75696 KB Output is correct
52 Correct 1018 ms 74540 KB Output is correct
53 Execution timed out 3017 ms 60476 KB Time limit exceeded
54 Halted 0 ms 0 KB -