답안 #536121

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
536121 2022-03-12T11:59:21 Z Alex_tz307 Parkovi (COCI22_parkovi) C++17
110 / 110
1244 ms 41988 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 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 5076 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 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 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 421 ms 34380 KB Output is correct
2 Correct 449 ms 35240 KB Output is correct
3 Correct 211 ms 19600 KB Output is correct
4 Correct 1101 ms 18836 KB Output is correct
5 Correct 1032 ms 18388 KB Output is correct
6 Correct 1111 ms 18404 KB Output is correct
7 Correct 1025 ms 17204 KB Output is correct
8 Correct 1126 ms 17860 KB Output is correct
9 Correct 1060 ms 18352 KB Output is correct
10 Correct 1185 ms 18552 KB Output is correct
11 Correct 842 ms 20724 KB Output is correct
12 Correct 791 ms 20864 KB Output is correct
13 Correct 888 ms 22352 KB Output is correct
14 Correct 767 ms 19304 KB Output is correct
15 Correct 781 ms 18808 KB Output is correct
16 Correct 797 ms 20532 KB Output is correct
17 Correct 783 ms 19496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 504 ms 35768 KB Output is correct
2 Correct 473 ms 39128 KB Output is correct
3 Correct 439 ms 37376 KB Output is correct
4 Correct 418 ms 37240 KB Output is correct
5 Correct 479 ms 41220 KB Output is correct
6 Correct 500 ms 40336 KB Output is correct
7 Correct 518 ms 41988 KB Output is correct
8 Correct 492 ms 39656 KB Output is correct
9 Correct 474 ms 39364 KB Output is correct
10 Correct 441 ms 38720 KB Output is correct
11 Correct 495 ms 37464 KB Output is correct
12 Correct 484 ms 41772 KB Output is correct
13 Correct 496 ms 41764 KB Output is correct
14 Correct 487 ms 40440 KB Output is correct
15 Correct 467 ms 38040 KB Output is correct
16 Correct 423 ms 36544 KB Output is correct
17 Correct 410 ms 36300 KB Output is correct
18 Correct 439 ms 38248 KB Output is correct
19 Correct 442 ms 38860 KB Output is correct
20 Correct 448 ms 39444 KB Output is correct
21 Correct 429 ms 38480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 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 5076 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 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 3 ms 4948 KB Output is correct
15 Correct 421 ms 34380 KB Output is correct
16 Correct 449 ms 35240 KB Output is correct
17 Correct 211 ms 19600 KB Output is correct
18 Correct 1101 ms 18836 KB Output is correct
19 Correct 1032 ms 18388 KB Output is correct
20 Correct 1111 ms 18404 KB Output is correct
21 Correct 1025 ms 17204 KB Output is correct
22 Correct 1126 ms 17860 KB Output is correct
23 Correct 1060 ms 18352 KB Output is correct
24 Correct 1185 ms 18552 KB Output is correct
25 Correct 842 ms 20724 KB Output is correct
26 Correct 791 ms 20864 KB Output is correct
27 Correct 888 ms 22352 KB Output is correct
28 Correct 767 ms 19304 KB Output is correct
29 Correct 781 ms 18808 KB Output is correct
30 Correct 797 ms 20532 KB Output is correct
31 Correct 783 ms 19496 KB Output is correct
32 Correct 504 ms 35768 KB Output is correct
33 Correct 473 ms 39128 KB Output is correct
34 Correct 439 ms 37376 KB Output is correct
35 Correct 418 ms 37240 KB Output is correct
36 Correct 479 ms 41220 KB Output is correct
37 Correct 500 ms 40336 KB Output is correct
38 Correct 518 ms 41988 KB Output is correct
39 Correct 492 ms 39656 KB Output is correct
40 Correct 474 ms 39364 KB Output is correct
41 Correct 441 ms 38720 KB Output is correct
42 Correct 495 ms 37464 KB Output is correct
43 Correct 484 ms 41772 KB Output is correct
44 Correct 496 ms 41764 KB Output is correct
45 Correct 487 ms 40440 KB Output is correct
46 Correct 467 ms 38040 KB Output is correct
47 Correct 423 ms 36544 KB Output is correct
48 Correct 410 ms 36300 KB Output is correct
49 Correct 439 ms 38248 KB Output is correct
50 Correct 442 ms 38860 KB Output is correct
51 Correct 448 ms 39444 KB Output is correct
52 Correct 429 ms 38480 KB Output is correct
53 Correct 1099 ms 18716 KB Output is correct
54 Correct 1107 ms 19116 KB Output is correct
55 Correct 1232 ms 19812 KB Output is correct
56 Correct 1147 ms 19468 KB Output is correct
57 Correct 1180 ms 20516 KB Output is correct
58 Correct 1085 ms 18848 KB Output is correct
59 Correct 1244 ms 22180 KB Output is correct
60 Correct 1186 ms 18252 KB Output is correct
61 Correct 1030 ms 17080 KB Output is correct
62 Correct 1051 ms 18564 KB Output is correct
63 Correct 1167 ms 18252 KB Output is correct
64 Correct 1131 ms 20164 KB Output is correct
65 Correct 1141 ms 18656 KB Output is correct
66 Correct 1135 ms 19420 KB Output is correct
67 Correct 1063 ms 17812 KB Output is correct
68 Correct 1238 ms 21816 KB Output is correct
69 Correct 469 ms 39400 KB Output is correct
70 Correct 421 ms 37020 KB Output is correct
71 Correct 501 ms 41020 KB Output is correct
72 Correct 190 ms 18804 KB Output is correct
73 Correct 188 ms 17580 KB Output is correct
74 Correct 203 ms 18856 KB Output is correct
75 Correct 781 ms 21404 KB Output is correct
76 Correct 844 ms 20496 KB Output is correct
77 Correct 799 ms 20676 KB Output is correct
78 Correct 815 ms 20568 KB Output is correct