tuki2501

fcb028_nearest.cpp

Nov 9th, 2021
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 500005;
  5.  
  6. int n, m, b, r;
  7. vector<int> adj[N];
  8. int vst[N], res[N];
  9.  
  10. int main() {
  11.   cin.tie(0)->sync_with_stdio(0);
  12.   cin >> n >> m >> b >> r;
  13.   queue<array<int,2>> q;
  14.   for (int i = 0; i < b; i++) {
  15.     int x; cin >> x;
  16.     q.push({0, x});
  17.   }
  18.   vector<int> p(r);
  19.   for (auto &i : p) {
  20.     cin >> i;
  21.   }
  22.   for (int i = 0; i < m; i++) {
  23.     int u, v;
  24.     cin >> u >> v;
  25.     adj[u].push_back(v);
  26.     adj[v].push_back(u);
  27.   }
  28.   while (q.size()) {
  29.     auto i = q.front(); q.pop();
  30.     if (vst[i[1]]) continue;
  31.     vst[i[1]] = 1;
  32.     res[i[1]] = i[0];
  33.     for (auto &j : adj[i[1]]) {
  34.       q.push({i[0] + 1, j});
  35.     }
  36.   }
  37.   for (auto &i : p) {
  38.     cout << res[i] << ' ';
  39.   }
  40.   cout << '\n';
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment