Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include<bits/stdc++.h>
- #include<iostream>
- #include<fstream>
- #include<vector>
- #include<queue>
- #include<set>
- #define long long long
- #define nln '\n'
- const long N = 5*1e5+10;
- using namespace std;
- // Global variables: f1, f2, n, m, b, r, mat, tic, cit
- fstream f1, f2;
- inline void openf()
- {
- f1.open("nearest.inp", ios:: in);
- f2.open("nearest.out", ios:: out);
- }
- inline void closef()
- {
- f1.close();
- f2.close();
- }
- long n, m, b, r;
- vector<set<long>> mat(N);
- vector<bool> tic;
- vector<long> cit;
- void data()
- {
- f1.tie(0)->sync_with_stdio(0);
- f2.tie(0)->sync_with_stdio(0);
- //cin.tie(0)->sync_with_stdio(0);
- cin >> n >> m >> b >> r;
- tic.resize(n+1, 0);
- for (long i = 0; i != b; ++i)
- {
- long x;
- cin >> x;
- tic[x] = 1;
- }
- for (long i = 0; i != r; ++i)
- {
- long x;
- cin >> x;
- cit.push_back(x);
- }
- ++n;
- for (long i = 0; i != m; ++i)
- {
- long x, y;
- cin >> x >> y;
- if (tic[x])
- x = n;
- if (tic[y])
- y = n;
- if (x != y)
- {
- mat[x].insert(y);
- mat[y].insert(x);
- }
- }
- }
- vector<long> lev;
- void bfs(long sta)
- {
- vector<bool> ava;
- queue<long> que;
- long fin;
- lev.resize(n+1, 0);
- ava.resize(n+1, 1);
- ava[sta] = 1;
- que.push(sta);
- while (!que.empty())
- {
- long run = que.front();
- que.pop();
- for (const auto &i : mat[run])
- if (ava[i])
- {
- lev[i] = lev[run] + 1;
- ava[i] = 0;
- que.push(i);
- }
- }
- }
- void process()
- {
- bfs(n);
- }
- void view()
- {
- for (const auto &i : cit)
- cout << lev[i] << ' ';
- cout << nln;
- }
- int main()
- {
- openf();
- data();
- process();
- view();
- closef();
- return 0;
- }
Add Comment
Please, Sign In to add comment