Submission #1003962

#TimeUsernameProblemLanguageResultExecution timeMemory
1003962vjudge1Pastiri (COI20_pastiri)C++17
0 / 100
1075 ms60368 KiB
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<vector<int>> graph(n); for (int i = 0; i < n-1; ++i) { int u, v; cin >> u >> v; --u; --v; graph[u].push_back(v); graph[v].push_back(u); } vector<bool> ovelha(n); for (int i = 0; i < k; ++i) { int u; cin >> u; ovelha[u - 1] = true; } vector<vector<int>> cover(n); function<pair<int, vector<int>> (int, int)> dfs = [&](int u, int p) { if (ovelha[u]) return make_pair(0, vector<int>{u}); pair<int, vector<int>> best = make_pair((int)1e9, vector<int>()); for (int v: graph[u]) if (v != p) { auto [d, xs] = dfs(v, u); if (d + 1 < best.first ) { // cover[v] = best.second; best = make_pair(d + 1, move(xs)); } else if (d + 1 == best.first) { if (best.second.size() > xs.size()) for (int x: xs) best.second.push_back(x); else { for (int x: best.second) xs.push_back(x); best.second = move(xs); } } // else cover[v] = xs; } // cerr << "DFS " << u << " " << best.first << "\n"; return best; }; for (int i = 0; i < n; ++i) cover[i] = dfs(i, i).second; // for (int i = 0; i < n; ++i) { // cerr << i << " -> "; // for (int x: cover[i]) // cerr << x << " "; // cerr << "\n"; // } vector<pair<int, int>> sz(n); for (int i = 0; i < n; ++i) sz[i] = make_pair(cover[i].size(), i); sort(sz.rbegin(), sz.rend()); vector<int> ans; vector<bool> taken(n); for (auto [_, idx]: sz) { // cerr << "-> " << idx << "\n"; bool ok = false; for (int x: cover[idx]) if (!taken[x]) ok = true; if (ok) { ans.push_back(idx); for (int x: cover[idx]) taken[x] = true; } } cout << ans.size() << "\n"; for (int x: ans) cout << x+1 << " "; cout << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...