Submission #1093137

# Submission time Handle Problem Language Result Execution time Memory
1093137 2024-09-26T02:36:49 Z vjudge1 Pastiri (COI20_pastiri) C++17
100 / 100
401 ms 67908 KB
#include <bits/stdc++.h>

// #define int int64_t

const int kMaxN = 5e5 + 5;

int n, k;
int p[kMaxN], dep[kMaxN], dis[kMaxN], id[kMaxN];
bool vis[kMaxN], del[kMaxN];
std::vector<int> G[kMaxN];

void bfs() {
  std::queue<int> q;
  for (int i = 1; i <= n; ++i)
    if (vis[i])
      q.emplace(i);
  for (; !q.empty();) {
    int u = q.front(); q.pop();
    for (auto v : G[u]) {
      if (!vis[v]) {
        dis[v] = dis[u] + 1, vis[v] = 1;
        q.emplace(v);
      }
    }
  }
}

void dfs1(int u, int fa) {
  p[u] = fa, dep[u] = dep[fa] + 1;
  for (auto v : G[u]) {
    if (v == fa) continue;
    dfs1(v, u);
  }
}

void dfs2(int u, int fa) {
  del[u] = 1;
  for (auto v : G[u]) {
    if (del[v] || v == fa || dis[v] != dis[u] - 1) continue;
    dfs2(v, u);
  }
}

void dickdreamer() {
  std::cin >> n >> k;
  for (int i = 1; i < n; ++i) {
    int u, v;
    std::cin >> u >> v;
    G[u].emplace_back(v), G[v].emplace_back(u);
  }
  for (int i = 1; i <= k; ++i) {
    std::cin >> id[i];
    vis[id[i]] = 1;
  }
  bfs(), dfs1(1, 0);
  std::sort(id + 1, id + 1 + n, [&] (int i, int j) { return dep[i] > dep[j]; });
  std::vector<int> vec;
  for (int c = 1; c <= k; ++c) {
    int i = id[c];
    if (del[i]) continue;
    for (; p[i] && dis[p[i]] == dis[i] + 1; i = p[i]) {}
    dfs2(i, 0);
    vec.emplace_back(i);
  }
  std::cout << vec.size() << '\n';
  for (auto x : vec) std::cout << x << ' ';
}

int32_t main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
  int T = 1;
  // std::cin >> T;
  while (T--) dickdreamer();
  // std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 110 ms 63240 KB Output is correct
2 Correct 131 ms 63648 KB Output is correct
3 Correct 140 ms 63568 KB Output is correct
4 Correct 174 ms 67908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12380 KB Output is correct
2 Correct 7 ms 12380 KB Output is correct
3 Correct 213 ms 40728 KB Output is correct
4 Correct 234 ms 42584 KB Output is correct
5 Correct 284 ms 40272 KB Output is correct
6 Correct 401 ms 42288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12124 KB Output is correct
2 Correct 6 ms 12128 KB Output is correct
3 Correct 6 ms 12124 KB Output is correct
4 Correct 6 ms 12124 KB Output is correct
5 Correct 6 ms 12316 KB Output is correct
6 Correct 6 ms 12124 KB Output is correct
7 Correct 6 ms 12124 KB Output is correct
8 Correct 5 ms 12124 KB Output is correct
9 Correct 5 ms 12124 KB Output is correct
10 Correct 5 ms 12124 KB Output is correct
11 Correct 5 ms 12292 KB Output is correct
12 Correct 5 ms 12124 KB Output is correct
13 Correct 5 ms 12304 KB Output is correct
14 Correct 7 ms 12632 KB Output is correct
15 Correct 6 ms 12124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 317 ms 40904 KB Output is correct
2 Correct 313 ms 40788 KB Output is correct
3 Correct 322 ms 42688 KB Output is correct
4 Correct 291 ms 40132 KB Output is correct
5 Correct 196 ms 37576 KB Output is correct
6 Correct 293 ms 45388 KB Output is correct
7 Correct 310 ms 44540 KB Output is correct
8 Correct 314 ms 44396 KB Output is correct
9 Correct 371 ms 40276 KB Output is correct
10 Correct 320 ms 40276 KB Output is correct
11 Correct 192 ms 42324 KB Output is correct
12 Correct 223 ms 44148 KB Output is correct
13 Correct 236 ms 45680 KB Output is correct
14 Correct 172 ms 43312 KB Output is correct
15 Correct 36 ms 17100 KB Output is correct
16 Correct 293 ms 38028 KB Output is correct
17 Correct 266 ms 40904 KB Output is correct
18 Correct 283 ms 37456 KB Output is correct
19 Correct 310 ms 47660 KB Output is correct
20 Correct 293 ms 50404 KB Output is correct
21 Correct 264 ms 43388 KB Output is correct
22 Correct 286 ms 43860 KB Output is correct