Advertisement
Guest User

Linhas de Ônibus

a guest
Dec 11th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include<iostream>
  2. #include<queue>
  3. #include<vector>
  4. #include<string.h>
  5. using namespace std;
  6.  
  7.  
  8. const int N = 6e2;
  9. vector<int> graph[2*N];
  10. int father[2*N];
  11. bool viz[2*N];
  12. int T, L, O, D, C, x;
  13.  
  14. void BFS(int O){
  15.     memset(father, -1, sizeof(father));
  16.     memset(viz, false, sizeof(viz));
  17.     int at, neighbor;
  18.     queue<int> q;
  19.     q.push(O);
  20.     while(q.size()){
  21.         at = q.front();
  22.         viz[at] = true;
  23.         q.pop();
  24.         for(int i=0; i < graph[at].size(); i++){
  25.             neighbor = graph[at][i];
  26.             if(!viz[neighbor]){
  27.                 q.push(neighbor);
  28.                 father[neighbor] = at;
  29.             }
  30.         }
  31.     }
  32. }
  33.  
  34. int solve(int O, int D){
  35.     int at = D;
  36.     int qtd = 0;
  37.     while(father[at] != O){
  38.         at = father[at];
  39.         qtd++;
  40.     }
  41.     return qtd;
  42. }
  43.  
  44.  
  45. int main(){
  46.     cin >> T >> L >> O >> D;   
  47.     for(int i=0; i < L; i++){
  48.         cin >> C;
  49.         for(int j=0; j < C; j++){
  50.             cin >> x;
  51.             graph[N+i].push_back(x);
  52.             graph[x].push_back(N+i);
  53.         }
  54.     }
  55.     BFS(O);
  56.    
  57.     cout << 1 + solve(O, D)/2 << endl;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement