Iamtui1010

nearest

Nov 8th, 2021 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. //#include<bits/stdc++.h>
  2. #include<iostream>
  3. #include<fstream>
  4. #include<vector>
  5. #include<queue>
  6. #include<set>
  7.  
  8. #define long long long
  9. #define nln '\n'
  10.  
  11. const long N = 5*1e5+10;
  12.  
  13. using namespace std;
  14.  
  15. // Global variables: f1, f2, n, m, b, r, mat, tic, cit
  16.  
  17. fstream f1, f2;
  18.  
  19. inline void openf()
  20. {
  21.     f1.open("nearest.inp", ios:: in);
  22.     f2.open("nearest.out", ios:: out);
  23. }
  24.  
  25. inline void closef()
  26. {
  27.     f1.close();
  28.     f2.close();
  29. }
  30.  
  31. long n, m, b, r;
  32. vector<set<long>> mat(N);
  33. vector<bool> tic;
  34. vector<long> cit;
  35.  
  36. void data()
  37. {
  38.     f1.tie(0)->sync_with_stdio(0);
  39.     f2.tie(0)->sync_with_stdio(0);
  40.     //cin.tie(0)->sync_with_stdio(0);
  41.  
  42.     cin >> n >> m >> b >> r;
  43.  
  44.     tic.resize(n+1, 0);
  45.  
  46.     for (long i = 0; i != b; ++i)
  47.     {
  48.         long x;
  49.         cin >> x;
  50.         tic[x] = 1;
  51.     }
  52.  
  53.     for (long i = 0; i != r; ++i)
  54.     {
  55.         long x;
  56.         cin >> x;
  57.         cit.push_back(x);
  58.     }
  59.  
  60.     ++n;
  61.     for (long i = 0; i != m; ++i)
  62.     {
  63.         long x, y;
  64.         cin >> x >> y;
  65.         if (tic[x])
  66.             x = n;
  67.         if (tic[y])
  68.             y = n;
  69.         if (x != y)
  70.         {
  71.             mat[x].insert(y);
  72.             mat[y].insert(x);
  73.         }
  74.     }
  75. }
  76.  
  77. vector<long> lev;
  78.  
  79. void bfs(long sta)
  80. {
  81.    
  82.     vector<bool> ava;
  83.     queue<long> que;
  84.     long fin;
  85.  
  86.     lev.resize(n+1, 0);
  87.     ava.resize(n+1, 1);
  88.     ava[sta] = 1;
  89.     que.push(sta);
  90.  
  91.     while (!que.empty())
  92.     {
  93.         long run = que.front();
  94.         que.pop();
  95.         for (const auto &i : mat[run])
  96.             if (ava[i])
  97.             {
  98.                 lev[i] = lev[run] + 1;
  99.                 ava[i] = 0;
  100.                 que.push(i);
  101.             }
  102.     }
  103. }
  104.  
  105. void process()
  106. {
  107.     bfs(n);
  108. }
  109.  
  110. void view()
  111. {
  112.     for (const auto &i : cit)
  113.         cout << lev[i] << ' ';
  114.     cout << nln;
  115. }
  116.  
  117. int main()
  118. {
  119.     openf();
  120.     data();
  121.     process();
  122.     view();
  123.     closef();
  124.     return 0;
  125. }
  126.  
Add Comment
Please, Sign In to add comment