Submission #1004088

#TimeUsernameProblemLanguageResultExecution timeMemory
1004088vjudge1Pastiri (COI20_pastiri)C++17
0 / 100
1027 ms182356 KiB
#include<bits/stdc++.h> using namespace std; const int N = 5e5+5; int n, k, dist[N], cnt[N]; vector<int> s, G[N], S[N], P[N], ans; bool p[N], seen[N]; set<pair<int,pair<int,int> > > st; void bfs() { queue<int> Q; for(int i = 1; i <= n; i ++) dist[i] = N + 20; for(int i : s) dist[i] = 0, cnt[i] = 1, Q.push(i), p[i] = true; while(Q.size()) { int u = Q.front(); // cerr << u << endl; Q.pop(); for(int v: G[u]) if(dist[u] + 1 <= dist[v]) { S[u].push_back(v); P[v].push_back(u); cnt[v] += cnt[u]; if(dist[v] > dist[u] + 1) { dist[v] = dist[u] + 1; Q.push(v); } } } } void dfs(int v) { if(seen[v]) return; seen[v] = true; for(int u : S[v]) { st.erase({cnt[u], {dist[u], u}}); cnt[v] -= cnt[u]; if(cnt[u] > 0) st.insert({cnt[u],{dist[u], u}}); } for(int u : P[v]) dfs(u); } void solve() { for(int i = 1; i <= n; i ++) st.insert({cnt[i], {dist[i], i}}); while(st.size() && st.rbegin()->first > 0) { int u = st.rbegin()->second.second; if(!seen[u]) ans.push_back(u); st.erase(--st.end()); dfs(u); } } int main() { cin >> n >> k; for(int i = 1; i < n; i ++) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } s.resize(k); for(int i = 0; i < k; i ++) cin >> s[i]; bfs(); solve(); cout << ans.size() << endl; for(int v : ans) cout << v << ' '; cout << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...