This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast,unroll-loops")
// el psy congroo
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ins insert
#define pii pair<int,int>
#define pb push_back
#define endl '\n'
#define putr(x) cout<<x<<endl;return;
#define all(x) x.begin(),x.end()
const int mod = 998244353, sze = 5e5+23, inf = LLONG_MAX, LG = 17;
vector<int> graph[sze];
int used[sze];
int dist[sze];
int d[sze];
int hig[sze];
void dfs(int node,int par=0,int mx=-23,int best=1){
if( (d[node] + dist[node] )> mx ){
mx=d[node]+dist[node];
best=node;
}
hig[node]=best;
for(auto v:graph[node]){
if(v!=par){
d[v]=d[node]+1;
dfs(v,node,mx,best);
}
}
}
void sil(int node,int par=0){
used[node]=1;
for(auto v:graph[node]){
if(!used[v] && dist[node]==dist[v]+1){
sil(v,node);
}
}
}
void fast(){
int n,k;
cin>>n>>k;
for(int i =1;i<n;i++){
int u,v;
cin>>u>>v;
graph[u].pb(v);
graph[v].pb(u);
}
for(int i =1;i<=n;i++){
dist[i]=inf;
}
queue<int> q;
vector<int> sheep(k);
for(auto &v:sheep){
cin>>v;
q.push(v);
dist[v]=0;
}
while(!q.empty()){
int node = q.front();q.pop();
for(auto v:graph[node]){
if(dist[v] > dist[node]+1){
dist[v]=dist[node]+1;
q.push(v);
}
}
}
dfs(1);
sort(all(sheep),[&](int a,int b){
return d[a]>d[b];
});
vector<int> ans;
for(auto v:sheep){
if(!used[v]){
sil(hig[v]);
ans.pb(hig[v]);
}
}
cout<<ans.size()<<"\n";
for(auto v:ans) cout<<v<<" ";cout<<endl;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
// cin>>tt;
while(tt--){
fast();
}
return 0;
}
Compilation message (stderr)
pastiri.cpp: In function 'void fast()':
pastiri.cpp:85:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
85 | for(auto v:ans) cout<<v<<" ";cout<<endl;
| ^~~
pastiri.cpp:85:34: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
85 | for(auto v:ans) cout<<v<<" ";cout<<endl;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |