Submission #536128

# Submission time Handle Problem Language Result Execution time Memory
536128 2022-03-12T12:11:29 Z Alex_tz307 Parkovi (COCI22_parkovi) C++17
110 / 110
1246 ms 40948 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int kN = 2e5;
const int INF = 1e18L;
vector<pair<int, int>> g[1 + kN];
int N, depth[1 + kN], sol[kN];
bitset<1 + kN> mark, covered;

void maxSelf(int &x, int y) {
  if (x < y) {
    x = y;
  }
}

void minSelf(int &x, int y) {
  if (y < x) {
    x = y;
  }
}

int dfs(int u, int par, int radius) {
  if ((int)g[u].size() == 1 && par) {
    return 0;
  }
  int mx = -INF, mn = INF;
  for (auto it : g[u]) {
    int v, w;
    tie(v, w) = it;
    if (v != par) {
      int ret = dfs(v, u, radius);
      if (ret == 0 && covered[v]) { // v este acoperit si nimic din subarborele sau nu ma mai poate ajuta mai mult decat u
        maxSelf(mx, 0);
        minSelf(mn, 0);
        continue;
      }
      if (ret < 0 && ret + w <= 0) { // u este acoperit
        covered[u] = true;
      }
      if (ret < 0 && ret + w > 0) { // v sigur e acoperit si incepand de la u nu se mai acopera nimic nou, deci "resetez"
        ret = 0;
      } else { // creste distanta fata de ultimul nod marcat si mai mult
        ret += w;
      }
      if (ret > radius) { // trebuie sa il marchez pe v acum ca sa acopere alte noduri nemarcate, pentru ca deja u nu poate ajuta
        sol[N++] = v;
        mark[v] = true;
        covered[v] = true;
        ret = min(-radius + w, 0LL); // aproape analog cu cazurile anterioare
        if (w <= radius) {
          covered[u] = true;
        }
      }
      maxSelf(mx, ret);
      minSelf(mn, ret);
    }
  }
  if (mx <= -mn) {
    return mn;
  }
  return mx;
}

bool check(int m, int k) {
  N = 0;
  mark.reset();
  covered.reset();
  if (dfs(1, 0, m) > 0) {
    sol[N++] = 1;
    mark[1] = true;
  } else if (!covered[1]) {
    sol[N++] = 1;
    mark[1] = true;
  }
  return N <= k;
}

void testCase() {
  int n, 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 ans = 0, step = (1LL << 60);
  while (step) {
    if (!check(ans | step, k)) {
      ans |= step;
    }
    step /= 2;
  }
  ans += 1;
  check(ans, k);
  for (int v = 1; v <= n && N < k; ++v) {
    if (!mark[v]) {
      sol[N++] = v;
    }
  }
  cout << ans << '\n';
  for (int i = 0; i < k; ++i) {
    cout << sol[i] << ' ';
  }
  cout << '\n';
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int tests = 1;
  for (int tc = 1; tc <= tests; ++tc) {
    testCase();
  }
  return 0;
}
# Verdict Execution time Memory 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 5028 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5028 KB Output is correct
12 Correct 3 ms 5028 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 502 ms 34336 KB Output is correct
2 Correct 474 ms 35180 KB Output is correct
3 Correct 298 ms 15040 KB Output is correct
4 Correct 1130 ms 14668 KB Output is correct
5 Correct 1085 ms 18480 KB Output is correct
6 Correct 1125 ms 18400 KB Output is correct
7 Correct 1113 ms 17196 KB Output is correct
8 Correct 1220 ms 17956 KB Output is correct
9 Correct 1246 ms 18248 KB Output is correct
10 Correct 1182 ms 18536 KB Output is correct
11 Correct 877 ms 20732 KB Output is correct
12 Correct 801 ms 20856 KB Output is correct
13 Correct 888 ms 22344 KB Output is correct
14 Correct 787 ms 19352 KB Output is correct
15 Correct 797 ms 18800 KB Output is correct
16 Correct 823 ms 20428 KB Output is correct
17 Correct 797 ms 19436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 520 ms 35740 KB Output is correct
2 Correct 484 ms 34892 KB Output is correct
3 Correct 429 ms 33360 KB Output is correct
4 Correct 414 ms 33296 KB Output is correct
5 Correct 567 ms 36932 KB Output is correct
6 Correct 534 ms 35964 KB Output is correct
7 Correct 510 ms 37420 KB Output is correct
8 Correct 475 ms 35972 KB Output is correct
9 Correct 524 ms 35664 KB Output is correct
10 Correct 448 ms 35044 KB Output is correct
11 Correct 431 ms 33940 KB Output is correct
12 Correct 518 ms 38160 KB Output is correct
13 Correct 549 ms 38212 KB Output is correct
14 Correct 488 ms 36700 KB Output is correct
15 Correct 457 ms 35148 KB Output is correct
16 Correct 445 ms 33704 KB Output is correct
17 Correct 436 ms 33664 KB Output is correct
18 Correct 504 ms 35364 KB Output is correct
19 Correct 427 ms 36068 KB Output is correct
20 Correct 444 ms 36620 KB Output is correct
21 Correct 473 ms 35612 KB Output is correct
# Verdict Execution time Memory 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 5028 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5028 KB Output is correct
12 Correct 3 ms 5028 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 502 ms 34336 KB Output is correct
16 Correct 474 ms 35180 KB Output is correct
17 Correct 298 ms 15040 KB Output is correct
18 Correct 1130 ms 14668 KB Output is correct
19 Correct 1085 ms 18480 KB Output is correct
20 Correct 1125 ms 18400 KB Output is correct
21 Correct 1113 ms 17196 KB Output is correct
22 Correct 1220 ms 17956 KB Output is correct
23 Correct 1246 ms 18248 KB Output is correct
24 Correct 1182 ms 18536 KB Output is correct
25 Correct 877 ms 20732 KB Output is correct
26 Correct 801 ms 20856 KB Output is correct
27 Correct 888 ms 22344 KB Output is correct
28 Correct 787 ms 19352 KB Output is correct
29 Correct 797 ms 18800 KB Output is correct
30 Correct 823 ms 20428 KB Output is correct
31 Correct 797 ms 19436 KB Output is correct
32 Correct 520 ms 35740 KB Output is correct
33 Correct 484 ms 34892 KB Output is correct
34 Correct 429 ms 33360 KB Output is correct
35 Correct 414 ms 33296 KB Output is correct
36 Correct 567 ms 36932 KB Output is correct
37 Correct 534 ms 35964 KB Output is correct
38 Correct 510 ms 37420 KB Output is correct
39 Correct 475 ms 35972 KB Output is correct
40 Correct 524 ms 35664 KB Output is correct
41 Correct 448 ms 35044 KB Output is correct
42 Correct 431 ms 33940 KB Output is correct
43 Correct 518 ms 38160 KB Output is correct
44 Correct 549 ms 38212 KB Output is correct
45 Correct 488 ms 36700 KB Output is correct
46 Correct 457 ms 35148 KB Output is correct
47 Correct 445 ms 33704 KB Output is correct
48 Correct 436 ms 33664 KB Output is correct
49 Correct 504 ms 35364 KB Output is correct
50 Correct 427 ms 36068 KB Output is correct
51 Correct 444 ms 36620 KB Output is correct
52 Correct 473 ms 35612 KB Output is correct
53 Correct 1075 ms 18728 KB Output is correct
54 Correct 1099 ms 19172 KB Output is correct
55 Correct 1205 ms 19724 KB Output is correct
56 Correct 1152 ms 19456 KB Output is correct
57 Correct 1186 ms 20436 KB Output is correct
58 Correct 1095 ms 18828 KB Output is correct
59 Correct 1243 ms 22108 KB Output is correct
60 Correct 1169 ms 18340 KB Output is correct
61 Correct 1010 ms 17068 KB Output is correct
62 Correct 1042 ms 18440 KB Output is correct
63 Correct 1173 ms 18320 KB Output is correct
64 Correct 1094 ms 20136 KB Output is correct
65 Correct 1204 ms 18648 KB Output is correct
66 Correct 1209 ms 19476 KB Output is correct
67 Correct 1067 ms 17852 KB Output is correct
68 Correct 1222 ms 21480 KB Output is correct
69 Correct 454 ms 39404 KB Output is correct
70 Correct 420 ms 37012 KB Output is correct
71 Correct 488 ms 40948 KB Output is correct
72 Correct 194 ms 18736 KB Output is correct
73 Correct 195 ms 17560 KB Output is correct
74 Correct 214 ms 18908 KB Output is correct
75 Correct 781 ms 21364 KB Output is correct
76 Correct 832 ms 20588 KB Output is correct
77 Correct 785 ms 20436 KB Output is correct
78 Correct 826 ms 20628 KB Output is correct