Submission #446839

#TimeUsernameProblemLanguageResultExecution timeMemory
446839MOUF_MAHMALATPastiri (COI20_pastiri)C++14
0 / 100
484 ms43512 KiB
#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); } cout<<ans.size()<<endl; for(auto z:ans) cout<<z<<" "; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...