Advertisement
Mirbek

Поджог

Jan 9th, 2022
1,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1e5 + 3;
  6.  
  7. int n, m;
  8. int used[N], dist[N], par[N];
  9. vector <int> g[N];
  10.  
  11. int main(){
  12.     cin >> n >> m;
  13.  
  14.     for (int i = 1; i <= m; i++) {
  15.         int a, b;
  16.         cin >> a >> b;
  17.         g[a].push_back(b);
  18.         g[b].push_back(a);
  19.     }
  20.  
  21.     for (int i = 1; i <= n; i++) {
  22.         dist[i] = -1;
  23.     }
  24.  
  25.     queue <int> q;
  26.     int k;
  27.     cin >> k;
  28.  
  29.     for (int i = 1; i <= k; i++) {
  30.         int start;
  31.         cin >> start;
  32.         dist[start] = 0;
  33.         used[start] = 1;
  34.         q.push(start);
  35.     }
  36.  
  37.     while (!q.empty()) {
  38.         int v = q.front();
  39.         q.pop();
  40.         for (int i = 0; i < g[v].size(); i++) {
  41.             int to = g[v][i];
  42.             if (!used[to]) {
  43.                 used[to] = 1;
  44.                 dist[to] = dist[v] + 1;
  45.                 q.push(to);
  46.             }
  47.         }
  48.     }
  49.  
  50.     int mx = 0;
  51.     for (int i = 1; i <= n; i++) {
  52.         mx = max(mx, dist[i]);
  53.     }
  54.  
  55.     cout << mx << endl;
  56.     int last;
  57.     for (int i = 1; i<= n; i++) {
  58.         if (dist[i] == mx) {
  59.             last = i;
  60.             break;
  61.         }
  62.     }
  63.  
  64.     cout << last << endl;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement