Submission #1004222

#TimeUsernameProblemLanguageResultExecution timeMemory
1004222vjudge1Pastiri (COI20_pastiri)C++17
0 / 100
503 ms66232 KiB
#include<bits/stdc++.h> using namespace std; const int N = 5e5+5; int n, k, dist[N], in[N]; vector<int> s, G[N], P[N], ans; bool p[N], seen[N]; void bfs() { queue<int> Q; for(int i = 1; i <= n; i ++) dist[i] = N + 20; for(int i : s) dist[i] = 0, Q.push(i), p[i] = true; while(Q.size()) { int u = Q.front(); Q.pop(); for(int v: G[u]) if(dist[u] + 1 <= dist[v]) { P[v].push_back(u); in[u]++; if(dist[v] > dist[u] + 1) { dist[v] = dist[u] + 1; Q.push(v); } } } } void solve() { queue<int> Q; for(int i = 1; i <= n; i ++) if(in[i] == 0) Q.push(i); while(Q.size()) { int u = Q.front(); Q.pop(); if(P[u].size() == 1) { in[P[u][0]]--; if(in[P[u][0]] == 0) Q.push(P[u][0]); } else ans.push_back(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...