답안 #536136

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
536136 2022-03-12T12:37:26 Z Alex_tz307 Parkovi (COCI22_parkovi) C++17
110 / 110
1300 ms 31312 KB
#include <bits/stdc++.h>

using namespace std;

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

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

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

int64_t dfs(int u, int par, int64_t radius) {
  if ((int)g[u].size() == 1 && par) {
    return 0;
  }
  int64_t mx = -INF, mn = INF;
  for (auto it : g[u]) {
    int v, w;
    tie(v, w) = it;
    if (v != par) {
      int64_t 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, 0LL);
        minSelf(mn, 0LL);
        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, (int64_t)0); // aproape analog cu cazurile anterioare
        if (w <= radius) {
          covered[u] = true;
        }
      }
      maxSelf(mx, ret);
      minSelf(mn, ret);
    }
  }
  if (mx <= -mn) { // nodul de unde trebuie sa incep sa marchez din cauza lui mx(ca s-ar termina) va fi ori acoperit ori voi mai putea amana o eventuala marcare a sa datorita lui mn
    return mn;
  }
  return mx;
}

bool check(int64_t 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);
  }
  int64_t ans = 0, step = (1LL << 50);
  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';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int tests = 1;
  for (int tc = 1; tc <= tests; ++tc) {
    testCase();
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5024 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 4 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 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 333 ms 28532 KB Output is correct
2 Correct 342 ms 29172 KB Output is correct
3 Correct 172 ms 13092 KB Output is correct
4 Correct 813 ms 12108 KB Output is correct
5 Correct 768 ms 11888 KB Output is correct
6 Correct 782 ms 11844 KB Output is correct
7 Correct 788 ms 11900 KB Output is correct
8 Correct 914 ms 12364 KB Output is correct
9 Correct 837 ms 12136 KB Output is correct
10 Correct 845 ms 12236 KB Output is correct
11 Correct 618 ms 13388 KB Output is correct
12 Correct 576 ms 13776 KB Output is correct
13 Correct 637 ms 14668 KB Output is correct
14 Correct 591 ms 13628 KB Output is correct
15 Correct 552 ms 13100 KB Output is correct
16 Correct 582 ms 13720 KB Output is correct
17 Correct 553 ms 13120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 368 ms 29600 KB Output is correct
2 Correct 363 ms 28920 KB Output is correct
3 Correct 340 ms 27740 KB Output is correct
4 Correct 333 ms 27596 KB Output is correct
5 Correct 360 ms 30368 KB Output is correct
6 Correct 382 ms 29664 KB Output is correct
7 Correct 387 ms 30728 KB Output is correct
8 Correct 344 ms 29772 KB Output is correct
9 Correct 353 ms 29444 KB Output is correct
10 Correct 333 ms 29020 KB Output is correct
11 Correct 330 ms 28328 KB Output is correct
12 Correct 394 ms 31312 KB Output is correct
13 Correct 390 ms 31292 KB Output is correct
14 Correct 399 ms 30284 KB Output is correct
15 Correct 334 ms 29068 KB Output is correct
16 Correct 343 ms 28024 KB Output is correct
17 Correct 322 ms 27952 KB Output is correct
18 Correct 358 ms 29132 KB Output is correct
19 Correct 371 ms 29616 KB Output is correct
20 Correct 388 ms 30216 KB Output is correct
21 Correct 381 ms 29376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5024 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 4 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 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 333 ms 28532 KB Output is correct
16 Correct 342 ms 29172 KB Output is correct
17 Correct 172 ms 13092 KB Output is correct
18 Correct 813 ms 12108 KB Output is correct
19 Correct 768 ms 11888 KB Output is correct
20 Correct 782 ms 11844 KB Output is correct
21 Correct 788 ms 11900 KB Output is correct
22 Correct 914 ms 12364 KB Output is correct
23 Correct 837 ms 12136 KB Output is correct
24 Correct 845 ms 12236 KB Output is correct
25 Correct 618 ms 13388 KB Output is correct
26 Correct 576 ms 13776 KB Output is correct
27 Correct 637 ms 14668 KB Output is correct
28 Correct 591 ms 13628 KB Output is correct
29 Correct 552 ms 13100 KB Output is correct
30 Correct 582 ms 13720 KB Output is correct
31 Correct 553 ms 13120 KB Output is correct
32 Correct 368 ms 29600 KB Output is correct
33 Correct 363 ms 28920 KB Output is correct
34 Correct 340 ms 27740 KB Output is correct
35 Correct 333 ms 27596 KB Output is correct
36 Correct 360 ms 30368 KB Output is correct
37 Correct 382 ms 29664 KB Output is correct
38 Correct 387 ms 30728 KB Output is correct
39 Correct 344 ms 29772 KB Output is correct
40 Correct 353 ms 29444 KB Output is correct
41 Correct 333 ms 29020 KB Output is correct
42 Correct 330 ms 28328 KB Output is correct
43 Correct 394 ms 31312 KB Output is correct
44 Correct 390 ms 31292 KB Output is correct
45 Correct 399 ms 30284 KB Output is correct
46 Correct 334 ms 29068 KB Output is correct
47 Correct 343 ms 28024 KB Output is correct
48 Correct 322 ms 27952 KB Output is correct
49 Correct 358 ms 29132 KB Output is correct
50 Correct 371 ms 29616 KB Output is correct
51 Correct 388 ms 30216 KB Output is correct
52 Correct 381 ms 29376 KB Output is correct
53 Correct 1041 ms 11996 KB Output is correct
54 Correct 1183 ms 12204 KB Output is correct
55 Correct 1106 ms 12504 KB Output is correct
56 Correct 1186 ms 12440 KB Output is correct
57 Correct 1280 ms 13052 KB Output is correct
58 Correct 1125 ms 12056 KB Output is correct
59 Correct 1288 ms 14284 KB Output is correct
60 Correct 1171 ms 12412 KB Output is correct
61 Correct 1046 ms 11824 KB Output is correct
62 Correct 1016 ms 12816 KB Output is correct
63 Correct 1214 ms 12520 KB Output is correct
64 Correct 1118 ms 14140 KB Output is correct
65 Correct 1099 ms 12360 KB Output is correct
66 Correct 1070 ms 12944 KB Output is correct
67 Correct 1068 ms 11872 KB Output is correct
68 Correct 1300 ms 14404 KB Output is correct
69 Correct 416 ms 29112 KB Output is correct
70 Correct 351 ms 27876 KB Output is correct
71 Correct 440 ms 30748 KB Output is correct
72 Correct 226 ms 12632 KB Output is correct
73 Correct 194 ms 12760 KB Output is correct
74 Correct 202 ms 13856 KB Output is correct
75 Correct 597 ms 14052 KB Output is correct
76 Correct 640 ms 14280 KB Output is correct
77 Correct 583 ms 13900 KB Output is correct
78 Correct 587 ms 14384 KB Output is correct