#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<double> cd;
const int MAXN = 5e5+10;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
vector<int> adj[MAXN];
int prof[MAXN], dist[MAXN], elim[MAXN], pai[MAXN];
void build(int ver, int prev=0){
prof[ver]=prof[prev]+1;
pai[ver]=prev;
for(auto u : adj[ver]){
if(u==prev) continue;
build(u, ver);
}
}
void dfs(int ver, int val, int lim, vector<int>& mark){
mark[ver]=1;
elim[ver]=1;
if(val==lim) return;
for(auto u : adj[ver]){
if(mark[u]) continue;
dfs(u, val+1, lim, mark);
}
}
void solve(){
int n, k; cin >> n >> k;
for(int i=0;i<n-1;i++){
int a, b; cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
for(int i=1;i<=n;i++)
dist[i]=-1;
vector<int> spec(k);
queue<int> q;
for(int i=0;i<k;i++){
cin >> spec[i];
q.push(spec[i]);
dist[spec[i]]=0;
}
while(!q.empty()){
int curr=q.front();
q.pop();
for(auto u : adj[curr]){
if(dist[u]==-1){
dist[u]=dist[curr]+1;
q.push(u);
}
}
}
build(1);
sort(all(spec), [&](int a, int b){ return prof[a]>prof[b]; });
vector<int> ans;
for(auto u : spec){
if(elim[u]) continue;
int at=u, d=0;
while(pai[at]!=0 && dist[pai[at]]==d+1){
d++;
at=pai[at];
}
ans.pb(at);
vector<int> mark(n+1, 0);
dfs(at, 0, d, mark);
}
cout << (int)ans.size() << "\n";
for(int i=0;i<(int)ans.size();i++) cout << ans[i] << " \n"[i==(int)ans.size()-1];
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
int tt=1;
//~ cin >> tt;
while(tt--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
59336 KB |
Output is correct |
2 |
Correct |
134 ms |
67272 KB |
Output is correct |
3 |
Correct |
150 ms |
67488 KB |
Output is correct |
4 |
Execution timed out |
1083 ms |
70016 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12376 KB |
Output is correct |
2 |
Correct |
5 ms |
12380 KB |
Output is correct |
3 |
Correct |
173 ms |
44664 KB |
Output is correct |
4 |
Correct |
190 ms |
47024 KB |
Output is correct |
5 |
Correct |
291 ms |
44288 KB |
Output is correct |
6 |
Correct |
699 ms |
46368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
6 ms |
12124 KB |
Output is correct |
3 |
Correct |
5 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
12220 KB |
Output is correct |
5 |
Correct |
5 ms |
12380 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Correct |
5 ms |
12124 KB |
Output is correct |
8 |
Correct |
5 ms |
12300 KB |
Output is correct |
9 |
Correct |
5 ms |
12124 KB |
Output is correct |
10 |
Correct |
5 ms |
12124 KB |
Output is correct |
11 |
Correct |
5 ms |
12124 KB |
Output is correct |
12 |
Correct |
4 ms |
12120 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
5 ms |
12376 KB |
Output is correct |
15 |
Correct |
5 ms |
12124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
433 ms |
38344 KB |
Output is correct |
2 |
Correct |
396 ms |
44972 KB |
Output is correct |
3 |
Execution timed out |
1068 ms |
47268 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |