fork download
  1. //NiceDuck
  2. #include "bits/stdc++.h"
  3. typedef long long ll;
  4. using namespace std;
  5. #define FILE "000"
  6. #define foru(i,a,b) for(int i=(int)(a); i<=(int)(b); ++i)
  7. #define ford(i,a,b) for(int i=(int)(a); i>=(int)(b); --i)
  8. #define fastio ios_base::sync_with_stdio(0);cin.tie(0);
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define pii pair<int,int>
  13. #define pil pair<int,ll>
  14. #define pli pair<ll,int>
  15. #define MOD 1000000007
  16. #define el "\n"
  17.  
  18. const int MAX=5e5+5,INF=1e9;
  19. int n,m,b,r,dist[MAX];
  20. vector<int> adj[MAX];
  21. vector<int> c;
  22.  
  23. int main()
  24. {
  25. fastio
  26. #ifndef ONLINE_JUDGE
  27. freopen(FILE ".inp","r",stdin);
  28. freopen(FILE ".out","w",stdout);
  29. #endif // ONLINE_JUDGE
  30.  
  31. cin>>n>>m>>b>>r;
  32. foru(i,1,n) dist[i]=INF;
  33. queue<int> q;
  34. foru(i,1,b)
  35. {
  36. int x; cin>>x;
  37. dist[x]=0;
  38. q.push(x);
  39. }
  40. foru(i,1,r)
  41. {
  42. int x; cin>>x;
  43. c.pb(x);
  44. }
  45. foru(i,1,m)
  46. {
  47. int u,v; cin>>u>>v;
  48. adj[u].pb(v); adj[v].pb(u);
  49. }
  50. while(!q.empty())
  51. {
  52. int u=q.front(); q.pop();
  53. for(int v:adj[u])
  54. {
  55. if(dist[v]>dist[u]+1)
  56. {
  57. dist[v]=dist[u]+1;
  58. q.push(v);
  59. }
  60. }
  61. }
  62. for(int x:c) cout<<dist[x]<<' ';
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0.01s 16068KB
stdin
Standard input is empty
stdout
Standard output is empty