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<bits/stdc++.h>
#define all(s) s.begin(),s.end()
using namespace std;
typedef int ll;
ll n,k,x,y,p[500009],d[500009];
vector<vector<ll> >v;
queue<ll>q;
bool b[500009];
vector<ll>ans;
ll gp(ll z)
{
if(p[z]==z)
return z;
return p[z]=gp(p[z]);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
v.resize(n+1);
for(ll i=1; i<n; i++)
{
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(ll i=1; i<=n; i++)
p[i]=i,d[i]=1e9;
while(k--)
{
cin>>x;
d[x]=0;
q.push(x);
}
while(!q.empty())
{
x=q.front();
q.pop();
for(auto z:v[x])
{
if(d[z]<d[x]+1)
continue;
p[x]=z;
if(d[z]>d[x]+1)
{
d[z]=d[x]+1;
q.push(z);
}
}
}
for(ll i=1; i<=n; i++)
{
x=gp(i);
if(b[x])
continue;
b[x]=1;
ans.push_back(x);
}
sort(all(ans));
cout<<ans.size()<<endl;
for(auto z:ans)
cout<<z<<" ";
return 0;
}
# | 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... |