Submission #312549

#TimeUsernameProblemLanguageResultExecution timeMemory
312549model_codePastiri (COI20_pastiri)C++17
100 / 100
860 ms80200 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e5 + 5; int N, K; int sheep[MAXN]; int sorted[MAXN]; int cntDep[MAXN]; vector <int> adj[MAXN]; int dep[MAXN], dist[MAXN]; int highest[MAXN]; bool bio[MAXN]; void load() { scanf("%d%d", &N, &K); for (int i = 1; i < N; i++) { int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } for (int i = 0; i < K; i++) scanf("%d", sheep + i); } void dfs_init(int x, int p, int mx, int opt) { int sum = dep[x] + dist[x]; if (sum > mx) { mx = sum; opt = x; } highest[x] = opt; for (auto it : adj[x]) if (it != p) { dep[it] = dep[x] + 1; dfs_init(it, x, mx, opt); } } void bfs() { memset(dist, -1, sizeof dist); queue <int> Q; for (int i = 0; i < K; i++) { dist[sheep[i]] = 0; Q.push(sheep[i]); } while (!Q.empty()) { int curr = Q.front(); Q.pop(); for (auto it : adj[curr]) if (dist[it] == -1) { dist[it] = dist[curr] + 1; Q.push(it); } } } void dfs_cover(int x) { bio[x] = true; for (auto it : adj[x]) if (!bio[it] && dist[x] == dist[it] + 1) dfs_cover(it); } void sort_by_depth() { for (int i = 0; i < K; i++) cntDep[dep[sheep[i]] + 1]++; for (int i = 1; i <= N; i++) cntDep[i] += cntDep[i - 1]; for (int i = 0; i < K; i++) sorted[cntDep[dep[sheep[i]]]++] = sheep[i]; for (int i = 0; i < K; i++) sheep[i] = sorted[K - i - 1]; } void solve() { bfs(); dfs_init(1, 0, -1, 0); sort_by_depth(); vector <int> ans; for (int i = 0; i < K; i++) if (!bio[sheep[i]]) { ans.push_back(highest[sheep[i]]); dfs_cover(highest[sheep[i]]); } printf("%d\n", ans.size()); for (auto it : ans) printf("%d ", it); puts(""); } int main() { load(); solve(); return 0; }

Compilation message (stderr)

pastiri.cpp: In function 'void solve()':
pastiri.cpp:87:12: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wformat=]
   87 |   printf("%d\n", ans.size());
      |           ~^     ~~~~~~~~~~
      |            |             |
      |            int           std::vector<int>::size_type {aka long unsigned int}
      |           %ld
pastiri.cpp: In function 'void load()':
pastiri.cpp:16:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   16 |   scanf("%d%d", &N, &K);
      |   ~~~~~^~~~~~~~~~~~~~~~
pastiri.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   19 |     scanf("%d%d", &u, &v);
      |     ~~~~~^~~~~~~~~~~~~~~~
pastiri.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   24 |     scanf("%d", sheep + i);
      |     ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...