Submission #1262217

#TimeUsernameProblemLanguageResultExecution timeMemory
1262217minhpkPastiri (COI20_pastiri)C++20
0 / 100
393 ms177532 KiB
#include <bits/stdc++.h> #define int long long using namespace std; int a,b; vector <int> z[1000005]; vector <int> v; int high[1000005]; int par[500005][25]; bool cmp(int a,int b){ return high[a]>high[b]; } void dfs(int i,int parent){ for (auto p:z[i]){ if (p==parent){ continue; } high[p]=high[i]+1; par[p][0]=i; dfs(p,i); } } bool vis[1000005]; int cnt[1000005]; void bfs1(){ queue<int> q; for (auto p:v){ q.push(p); vis[p]=true; cnt[p]=0; } while (q.size()){ int pos=q.front(); q.pop(); for (auto p:z[pos]){ if (vis[p]){ continue; } vis[p]=true; cnt[p]=cnt[pos]+1; q.push(p); } } } vector <int> res; int jump(int x){ for (int i=20;i>=0;i--){ int nxt=par[x][i]; if (cnt[nxt]==high[x]-high[nxt]){ x=par[x][i]; } } return x; } void bfs(int sta,int len){ queue<pair<int,int>> q; q.push({sta,len}); vis[sta]=true; while (q.size()){ auto [pos,remain]=q.front(); q.pop(); if (remain==0){ continue; } for (auto p:z[pos]){ if (vis[p]){ continue; } vis[p]=true; q.push({p,remain-1}); } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> a >> b; for (int i=1;i<a;i++){ int x,y; cin >> x >> y; z[x].push_back(y); z[y].push_back(x); } for (int i=1;i<=b;i++){ int x; cin >> x; v.push_back(x); } high[0]=-1; par[1][0]=1; dfs(1,1); for (int j=1;j<=20;j++){ for (int i=1;i<=a;i++){ par[i][j]=par[par[i][j-1]][j-1]; } } sort(v.begin(),v.end(),cmp); bfs1(); memset(vis,false,sizeof vis); for (auto p:v){ if (vis[p]){ continue; } int nxt=jump(p); int len=high[p]-high[nxt]; res.push_back(nxt); bfs(nxt,len); } cout << res.size() << "\n"; for (auto p:res){ cout << p << " "; } 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...