답안 #827527

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
827527 2023-08-16T14:21:43 Z Kubogi Parkovi (COCI22_parkovi) C++17
110 / 110
1245 ms 40468 KB
// 11 ptt when ?
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fileio(name) if (fopen(name".inp", "r")) freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout)

const int maxn = 2e5+4, inf = 1e18;
int n, k;
vector<pair<int, int> > adj[maxn];

int f[maxn], g[maxn];
bool chosen[maxn];
void dfs(int u, int p, int val, int wp) {
    for (auto e: adj[u]) {
        int v = e.first, w = e.second;
        if (v != p) {
            dfs(v, u, val, w);
            f[u] = max(f[u], w + f[v]);
            g[u] = min(g[u], w + g[v]);
        }
    }
    if (f[u] + g[u] > val) {
        if (f[u] + wp > val || (u == 1 && f[u] >= 0)) {
            chosen[u] = true;
            f[u] = -inf, g[u] = 0;
        }
    } else {
        f[u] = -inf;
    }
}

int cnt(int val) {
    fill(f+1, f+1+n, 0);
    fill(g+1, g+1+n, inf);
    fill(chosen+1, chosen+1+n, false);
    dfs(1, 0, val, -inf);

    int res = 0;
    for (int i = 1; i <= n; i++) {
        res += chosen[i];
    }
    return res;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    fileio("ktree");
    // freopen("debug.txt", "w", stderr);

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

    int l = 0, r = 1e15;
    while (l <= r) {
        int mid = (l+r)>>1;
        if (cnt(mid) <= k) r = mid-1;
        else l = mid+1;
    }
    cout << l << "\n";
    int x = cnt(l);
    for (int i = 1; i <= n; i++) {
        if (chosen[i]) cout << i << " ";
        else if (x < k) {
            cout << i << " ";
            x++;
        }
    }

    return 0 ^ 0;
}

Compilation message

Main.cpp: In function 'int32_t main()':
Main.cpp:6:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 | #define fileio(name) if (fopen(name".inp", "r")) freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout)
      |                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:49:5: note: in expansion of macro 'fileio'
   49 |     fileio("ktree");
      |     ^~~~~~
Main.cpp:6:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 | #define fileio(name) if (fopen(name".inp", "r")) freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout)
      |                                                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:49:5: note: in expansion of macro 'fileio'
   49 |     fileio("ktree");
      |     ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 288 ms 34544 KB Output is correct
2 Correct 326 ms 35276 KB Output is correct
3 Correct 270 ms 17588 KB Output is correct
4 Correct 817 ms 17868 KB Output is correct
5 Correct 762 ms 17328 KB Output is correct
6 Correct 753 ms 17344 KB Output is correct
7 Correct 741 ms 17436 KB Output is correct
8 Correct 838 ms 17996 KB Output is correct
9 Correct 850 ms 17764 KB Output is correct
10 Correct 857 ms 18084 KB Output is correct
11 Correct 676 ms 19344 KB Output is correct
12 Correct 575 ms 23372 KB Output is correct
13 Correct 709 ms 25200 KB Output is correct
14 Correct 571 ms 22080 KB Output is correct
15 Correct 567 ms 21532 KB Output is correct
16 Correct 670 ms 23036 KB Output is correct
17 Correct 553 ms 22308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 334 ms 35788 KB Output is correct
2 Correct 300 ms 35028 KB Output is correct
3 Correct 279 ms 33404 KB Output is correct
4 Correct 290 ms 33464 KB Output is correct
5 Correct 309 ms 35660 KB Output is correct
6 Correct 348 ms 35388 KB Output is correct
7 Correct 357 ms 36412 KB Output is correct
8 Correct 303 ms 36064 KB Output is correct
9 Correct 322 ms 35748 KB Output is correct
10 Correct 309 ms 35104 KB Output is correct
11 Correct 310 ms 34076 KB Output is correct
12 Correct 351 ms 36684 KB Output is correct
13 Correct 361 ms 36980 KB Output is correct
14 Correct 356 ms 36168 KB Output is correct
15 Correct 296 ms 35132 KB Output is correct
16 Correct 276 ms 33868 KB Output is correct
17 Correct 280 ms 33804 KB Output is correct
18 Correct 299 ms 35292 KB Output is correct
19 Correct 354 ms 34780 KB Output is correct
20 Correct 315 ms 35500 KB Output is correct
21 Correct 298 ms 34892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 288 ms 34544 KB Output is correct
16 Correct 326 ms 35276 KB Output is correct
17 Correct 270 ms 17588 KB Output is correct
18 Correct 817 ms 17868 KB Output is correct
19 Correct 762 ms 17328 KB Output is correct
20 Correct 753 ms 17344 KB Output is correct
21 Correct 741 ms 17436 KB Output is correct
22 Correct 838 ms 17996 KB Output is correct
23 Correct 850 ms 17764 KB Output is correct
24 Correct 857 ms 18084 KB Output is correct
25 Correct 676 ms 19344 KB Output is correct
26 Correct 575 ms 23372 KB Output is correct
27 Correct 709 ms 25200 KB Output is correct
28 Correct 571 ms 22080 KB Output is correct
29 Correct 567 ms 21532 KB Output is correct
30 Correct 670 ms 23036 KB Output is correct
31 Correct 553 ms 22308 KB Output is correct
32 Correct 334 ms 35788 KB Output is correct
33 Correct 300 ms 35028 KB Output is correct
34 Correct 279 ms 33404 KB Output is correct
35 Correct 290 ms 33464 KB Output is correct
36 Correct 309 ms 35660 KB Output is correct
37 Correct 348 ms 35388 KB Output is correct
38 Correct 357 ms 36412 KB Output is correct
39 Correct 303 ms 36064 KB Output is correct
40 Correct 322 ms 35748 KB Output is correct
41 Correct 309 ms 35104 KB Output is correct
42 Correct 310 ms 34076 KB Output is correct
43 Correct 351 ms 36684 KB Output is correct
44 Correct 361 ms 36980 KB Output is correct
45 Correct 356 ms 36168 KB Output is correct
46 Correct 296 ms 35132 KB Output is correct
47 Correct 276 ms 33868 KB Output is correct
48 Correct 280 ms 33804 KB Output is correct
49 Correct 299 ms 35292 KB Output is correct
50 Correct 354 ms 34780 KB Output is correct
51 Correct 315 ms 35500 KB Output is correct
52 Correct 298 ms 34892 KB Output is correct
53 Correct 819 ms 21868 KB Output is correct
54 Correct 809 ms 22260 KB Output is correct
55 Correct 1032 ms 23008 KB Output is correct
56 Correct 1006 ms 22604 KB Output is correct
57 Correct 938 ms 22804 KB Output is correct
58 Correct 886 ms 21908 KB Output is correct
59 Correct 959 ms 23724 KB Output is correct
60 Correct 947 ms 21496 KB Output is correct
61 Correct 695 ms 20044 KB Output is correct
62 Correct 720 ms 20476 KB Output is correct
63 Correct 970 ms 21596 KB Output is correct
64 Correct 788 ms 21632 KB Output is correct
65 Correct 855 ms 21836 KB Output is correct
66 Correct 899 ms 21696 KB Output is correct
67 Correct 1014 ms 20848 KB Output is correct
68 Correct 1245 ms 23172 KB Output is correct
69 Correct 344 ms 39548 KB Output is correct
70 Correct 313 ms 37032 KB Output is correct
71 Correct 390 ms 40468 KB Output is correct
72 Correct 220 ms 21164 KB Output is correct
73 Correct 235 ms 20436 KB Output is correct
74 Correct 280 ms 21224 KB Output is correct
75 Correct 606 ms 23956 KB Output is correct
76 Correct 701 ms 23484 KB Output is correct
77 Correct 636 ms 23268 KB Output is correct
78 Correct 632 ms 22836 KB Output is correct