#include<bits/stdc++.h>
#define all(s) s.begin(),s.end()
using namespace std;
typedef int ll;
typedef pair<ll,ll>pll;
ll n,x,y,k,d[500009],a[500009],h[500009];
pll op[500009];
vector<vector<ll> >v;
queue<pll>q;
vector<ll>ans;
bool b[500009];
void dfs(ll u,ll p)
{
h[u]=h[p]+1;
for(auto z:v[u])
if(z!=p)
dfs(z,u);
}
bool cmp(ll xo,ll yo)
{
return h[xo]>h[yo];
}
void go(ll u,ll c,ll p)
{
b[u]=1;
if(c==0)
return;
for(auto z:v[u])
if(z!=p)
go(z,c-1,u);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
scanf("%d%d",&n,&k);
v.resize(n+1);
for(ll i=1; i<n; i++)
{
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
for(ll i=1; i<=n; i++)
d[i]=1e9,op[i]= {1e9,1e9};
for(ll i=1; i<=k; i++)
scanf("%d",&a[i]),d[a[i]]=0,q.push({a[i],a[i]});
dfs(1,0);
while(!q.empty())
{
x=q.front().first;
y=q.front().second;
q.pop();
op[y]=min(op[y], {h[x],x});
for(auto z:v[x])
{
if(d[z]<d[x]+1)
continue;
d[z]=d[x]+1;
q.push({z,y});
}
}
sort(a+1,a+k+1,cmp);
for(ll i=1; i<=k; i++)
{
if(b[a[i]])
continue;
ans.push_back(op[a[i]].second);
go(op[a[i]].second,h[a[i]]-op[a[i]].first,0);
}
cout<<ans.size()<<endl;
for(auto z:ans)
cout<<z<<" ";
return 0;
}
Compilation message
pastiri.cpp: In function 'int main()':
pastiri.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d%d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
pastiri.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
pastiri.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d",&a[i]),d[a[i]]=0,q.push({a[i],a[i]});
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
209 ms |
59088 KB |
Output is correct |
2 |
Correct |
268 ms |
59456 KB |
Output is correct |
3 |
Correct |
257 ms |
59560 KB |
Output is correct |
4 |
Correct |
393 ms |
65196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
716 KB |
Output is correct |
2 |
Correct |
3 ms |
588 KB |
Output is correct |
3 |
Correct |
540 ms |
37520 KB |
Output is correct |
4 |
Correct |
603 ms |
42776 KB |
Output is correct |
5 |
Correct |
677 ms |
36084 KB |
Output is correct |
6 |
Execution timed out |
1095 ms |
44740 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
440 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
4 ms |
844 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
9 ms |
2172 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
607 ms |
37136 KB |
Output is correct |
2 |
Correct |
626 ms |
43752 KB |
Output is correct |
3 |
Correct |
696 ms |
48292 KB |
Output is correct |
4 |
Correct |
594 ms |
42636 KB |
Output is correct |
5 |
Execution timed out |
1088 ms |
38844 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |