#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],c;
pll op[500009];
vector<vector<ll> >v;
queue<ll>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];
}
struct node
{
ll f,s,t;
};
queue<node>dq;
int main()
{
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]);
dfs(1,0);
while(!q.empty())
{
x=q.front();
q.pop();
for(auto z:v[x])
{
if(d[z]<=d[x]+1)
continue;
d[z]=d[x]+1;
q.push(z);
}
}
dq.push({1,0,1});
while(!dq.empty())
{
x=dq.front().f;
ll p=dq.front().s;
ll id=dq.front().t;
dq.pop();
if(d[x]==0)
op[x]= {h[id],id};
for(auto z:v[x])
{
if(z==p)
continue;
if(d[x]==d[z]+1)
dq.push({z,x,id});
else
dq.push({z,x,z});
}
}
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);
dq.push({op[a[i]].second,h[a[i]]-op[a[i]].first,0});
while(!dq.empty())
{
x=dq.front().f;
c=dq.front().s;
ll p=dq.front().t;
dq.pop();
b[x]=1;
if(c==0)
continue;
for(auto z:v[x])
if(z!=p)
dq.push({z,c-1,x});
}
}
cout<<ans.size()<<endl;
for(auto z:ans)
cout<<z<<" ";
return 0;
}
Compilation message
pastiri.cpp: In function 'int main()':
pastiri.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | scanf("%d%d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
pastiri.cpp:34:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
pastiri.cpp:41:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d",&a[i]),d[a[i]]=0,q.push(a[i]);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
59100 KB |
Output is correct |
2 |
Correct |
256 ms |
59456 KB |
Output is correct |
3 |
Correct |
257 ms |
59460 KB |
Output is correct |
4 |
Correct |
373 ms |
64276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
588 KB |
Output is correct |
2 |
Correct |
3 ms |
588 KB |
Output is correct |
3 |
Correct |
542 ms |
37956 KB |
Output is correct |
4 |
Correct |
550 ms |
41344 KB |
Output is correct |
5 |
Correct |
691 ms |
35944 KB |
Output is correct |
6 |
Execution timed out |
1080 ms |
38076 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
2 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 |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
8 ms |
460 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 |
620 ms |
36876 KB |
Output is correct |
2 |
Correct |
621 ms |
36744 KB |
Output is correct |
3 |
Correct |
700 ms |
39096 KB |
Output is correct |
4 |
Correct |
648 ms |
35912 KB |
Output is correct |
5 |
Correct |
502 ms |
32316 KB |
Output is correct |
6 |
Correct |
660 ms |
42256 KB |
Output is correct |
7 |
Correct |
715 ms |
41148 KB |
Output is correct |
8 |
Correct |
741 ms |
41028 KB |
Output is correct |
9 |
Execution timed out |
1059 ms |
36164 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |