#include <bits/stdc++.h>
using namespace std;
typedef long long lo;
#define fi first
#define se second
#define endl "\n"
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)
const lo inf = 1000000000;
const lo li = 500005;
const lo mod = 1000000007;
int n,m,a[li],k,flag,t,dep[li],dist[li],git[li],cevv[li],tut[li],no[li],tut2[li];
int cev;
string s;
vector<int> v[li],vv;
inline int in(){
int x;
cin>>x;
return x;
}
inline void dfs(int node,int ata){
dep[node]=dep[ata]+1;
for(auto go:v[node]){
if(go==ata)continue;
dfs(go,node);
}
}
int main(void){
scanf("%d %d",&n,&k);
for(int i=1;i<n;i++){
int x,y;
scanf("%d %d",&x,&y);
v[x].pb(y);
v[y].pb(x);
}
for(int i=1;i<=k;i++){
scanf("%d",&t);
a[t]=1;
}
dfs(1,0);
priority_queue<pair<int,pair<int,pair<int,int>>>> q;
FOR{
cevv[i]=1000000000;
if(a[i]){q.push({0,{dep[i],{i,i}}});}
dist[i]=-1;
}
cev=0;
while(q.size()){
int node=q.top().se.se.fi;
int co=-q.top().fi;
int depth=q.top().se.fi;
int bas=q.top().se.se.se;
q.pop();
if(no[bas])continue;
if(dist[node]==-1){
dist[node]=co;
tut[node]=depth;
tut2[node]=bas;
//~ cout<<node<<" :: "<<bas<<" :: "<<co<<" :: "<<depth<<endl;
if(cevv[bas]>dep[node]){
cevv[bas]=dep[node];
git[bas]=node;
}
for(auto go:v[node]){
if(dist[go]==-1)q.push({-co-1,{depth,{go,bas}}});
}
}
else if(dist[node]==co && tut2[node]!=bas){no[bas]=1;}
}
FOR{
if(cevv[i]!=1000000000 && no[i]==0)cev++;
}
printf("%d\n",cev);
FOR{
if(cevv[i]!=1000000000 && no[i]==0)vv.pb(git[i]);
}
sort(vv.begin(),vv.end());
for(int i=0;i<(int)vv.size();i++){
printf("%d ",vv[i]);
}
return 0;
}
Compilation message
pastiri.cpp: In function 'int main()':
pastiri.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d %d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~~
pastiri.cpp:43:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d %d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~~
pastiri.cpp:48:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%d",&t);
| ~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
53116 KB |
Output is correct |
2 |
Incorrect |
212 ms |
57260 KB |
Sheep 4883 not protected |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
12372 KB |
Sheep 3030 not protected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
12224 KB |
Sheep 58 not protected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
458 ms |
44424 KB |
Sheep 54 not protected |
2 |
Halted |
0 ms |
0 KB |
- |