답안 #764977

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
764977 2023-06-24T07:06:22 Z dxz05 Parkovi (COCI22_parkovi) C++17
50 / 110
3000 ms 84452 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);
        }
    }
}

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

    vector<bool> deleted(n + 1, false);

    function<void(int, int, ll)> 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;
            }
        }
    };

    vector<int> ans;
    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];
            }
        }

        ans.push_back(v);
        dfs(v, -1, d);
    }

    return ans;
}

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);

    vector<int> ans;

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

        vector<int> p = parks(m);

        if (p.size() <= k){
            ans = p;
            r = m - 1;
        } else {
            l = m + 1;
        }
    }


    cout << ++r << endl;
    for (int x : ans) cout << x << ' ';

    int x = k - (int) ans.size();
    for (int i = 1; i <= n; i++){
        if (x == 0) break;
        int j = lower_bound(all(ans), i) - ans.begin();
        if (j == ans.size() || 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 'void solve()':
Main.cpp:119:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  119 |         if (p.size() <= k){
      |             ~~~~~~~~~^~~~
Main.cpp:135:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |         if (j == ans.size() || ans[j] != i){
      |             ~~^~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:145:13: warning: unused variable 'startTime' [-Wunused-variable]
  145 |     clock_t startTime = clock();
      |             ^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 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 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5000 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1076 ms 79628 KB Output is correct
2 Correct 1177 ms 81776 KB Output is correct
3 Correct 1969 ms 63972 KB Output is correct
4 Correct 2193 ms 60372 KB Output is correct
5 Correct 2135 ms 58572 KB Output is correct
6 Correct 2027 ms 58688 KB Output is correct
7 Correct 2409 ms 59268 KB Output is correct
8 Correct 2522 ms 61812 KB Output is correct
9 Correct 2545 ms 60444 KB Output is correct
10 Correct 2646 ms 61552 KB Output is correct
11 Correct 1939 ms 63744 KB Output is correct
12 Correct 1864 ms 61852 KB Output is correct
13 Correct 2021 ms 66564 KB Output is correct
14 Correct 1827 ms 61336 KB Output is correct
15 Correct 1831 ms 61532 KB Output is correct
16 Correct 1872 ms 62348 KB Output is correct
17 Correct 1860 ms 61492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1012 ms 82820 KB Output is correct
2 Correct 976 ms 81132 KB Output is correct
3 Correct 960 ms 77196 KB Output is correct
4 Correct 1046 ms 77268 KB Output is correct
5 Correct 1047 ms 81464 KB Output is correct
6 Correct 1256 ms 81208 KB Output is correct
7 Correct 1132 ms 83808 KB Output is correct
8 Correct 1031 ms 83448 KB Output is correct
9 Correct 1002 ms 82812 KB Output is correct
10 Correct 1028 ms 81392 KB Output is correct
11 Correct 1070 ms 78712 KB Output is correct
12 Correct 1173 ms 83704 KB Output is correct
13 Correct 1165 ms 84452 KB Output is correct
14 Correct 1198 ms 83364 KB Output is correct
15 Correct 1061 ms 81324 KB Output is correct
16 Correct 1010 ms 78336 KB Output is correct
17 Correct 1035 ms 77992 KB Output is correct
18 Correct 1116 ms 81900 KB Output is correct
19 Correct 1061 ms 79472 KB Output is correct
20 Correct 1079 ms 81520 KB Output is correct
21 Correct 1059 ms 80244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 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 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 5000 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 1076 ms 79628 KB Output is correct
16 Correct 1177 ms 81776 KB Output is correct
17 Correct 1969 ms 63972 KB Output is correct
18 Correct 2193 ms 60372 KB Output is correct
19 Correct 2135 ms 58572 KB Output is correct
20 Correct 2027 ms 58688 KB Output is correct
21 Correct 2409 ms 59268 KB Output is correct
22 Correct 2522 ms 61812 KB Output is correct
23 Correct 2545 ms 60444 KB Output is correct
24 Correct 2646 ms 61552 KB Output is correct
25 Correct 1939 ms 63744 KB Output is correct
26 Correct 1864 ms 61852 KB Output is correct
27 Correct 2021 ms 66564 KB Output is correct
28 Correct 1827 ms 61336 KB Output is correct
29 Correct 1831 ms 61532 KB Output is correct
30 Correct 1872 ms 62348 KB Output is correct
31 Correct 1860 ms 61492 KB Output is correct
32 Correct 1012 ms 82820 KB Output is correct
33 Correct 976 ms 81132 KB Output is correct
34 Correct 960 ms 77196 KB Output is correct
35 Correct 1046 ms 77268 KB Output is correct
36 Correct 1047 ms 81464 KB Output is correct
37 Correct 1256 ms 81208 KB Output is correct
38 Correct 1132 ms 83808 KB Output is correct
39 Correct 1031 ms 83448 KB Output is correct
40 Correct 1002 ms 82812 KB Output is correct
41 Correct 1028 ms 81392 KB Output is correct
42 Correct 1070 ms 78712 KB Output is correct
43 Correct 1173 ms 83704 KB Output is correct
44 Correct 1165 ms 84452 KB Output is correct
45 Correct 1198 ms 83364 KB Output is correct
46 Correct 1061 ms 81324 KB Output is correct
47 Correct 1010 ms 78336 KB Output is correct
48 Correct 1035 ms 77992 KB Output is correct
49 Correct 1116 ms 81900 KB Output is correct
50 Correct 1061 ms 79472 KB Output is correct
51 Correct 1079 ms 81520 KB Output is correct
52 Correct 1059 ms 80244 KB Output is correct
53 Execution timed out 3083 ms 59996 KB Time limit exceeded
54 Halted 0 ms 0 KB -