This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define forn(i, n) for(int i=0; i<(int)n; ++i)
#define PB push_back
const int MAXN = 500010;
int n, m, dis[MAXN], par[MAXN], dep[MAXN], ord[MAXN];
bool vis[MAXN], del[MAXN], has[MAXN];
vector<int> g[MAXN], ans;
queue<int> Q;
void bfs(){
while(!Q.empty()){
int v= Q.front();
Q.pop();
for(auto to:g[v]){
if(vis[to]) continue;
dis[to]=dis[v]+1;
vis[to]=true;
Q.push(to);
}
}
}
void dfs(int v, int p){
par[v]=p;
for(auto to:g[v]){
if(to==p) continue;
dep[to]=dep[v]+1;
dfs(to, v);
}
}
void dele(int v, int p){
del[v]=true;
for(auto to:g[v]){
if(to==p) continue;
if(dis[to]<dis[v]) dele(to, v);
}
}
int main(){
scanf("%d %d", &n, &m);
forn(i, n-1){
int a, b; scanf("%d %d", &a, &b);
--a, --b;
g[a].PB(b), g[b].PB(a);
}
forn(i, m){
int a; scanf("%d", &a);
--a;
vis[a]=has[a]=true, Q.push(a);
}
bfs();
dfs(0, 0);
forn(i, n) ord[i]=i;
sort(ord, ord+n, [](int a, int b){
return dep[a]>dep[b];
});
forn(index, n){
int v = ord[index];
if(!has[v] || del[v]) continue;
int cur=v;
while(dis[cur]<dis[par[cur]]) cur = par[cur];
ans.PB(cur);
dele(cur, cur);
}
printf("%d\n", (int)ans.size());
for(auto el:ans) printf("%d ", el+1);
}
Compilation message (stderr)
pastiri.cpp: In function 'int main()':
pastiri.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
pastiri.cpp:49:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | int a, b; scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
pastiri.cpp:55:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | int a; scanf("%d", &a);
| ~~~~~^~~~~~~~~~
# | 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... |