Advertisement
Guest User

Untitled

a guest
May 26th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n, m, from, to, e;
  4. vector < vector <int> > v;
  5.  
  6. int bfs(int cur) {
  7.     queue<int> q;
  8.     q.push(cur);
  9.     vector <bool> used(n, false);
  10.     vector <int> d(n, 0);
  11.     used[cur] = true;
  12.     while(!q.empty()) {
  13.         int now = q.front();
  14.         q.pop();
  15.         for (int i = 0; i < v[now].size(); i++) {
  16.             if (!used[v[now][i]]) {
  17.                 used[v[now][i]] = true;
  18.                 q.push(v[now][i]);
  19.                 d[v[now][i]] = d[now] + 1;
  20.                 if(d[v[now][i]] == 3) break;
  21.             }
  22.         }
  23.     }
  24.     return count(d.begin(), d.end(), 2);
  25. }
  26.  
  27. int main () {
  28.     ios::sync_with_stdio(0);
  29.     cin.tie(0);
  30.     cin >> n >> m;
  31.     char c;
  32.     v.resize(n);
  33.     for (int i = 0; i < m; i++) {
  34.         cin >> c;
  35.         if (c == '+') {
  36.             cin >> from >> to;
  37.             from--; to--;
  38.             v[from].push_back(to);
  39.         } else {
  40.             cin >> e;
  41.             e--;
  42.             cout << bfs(e) << '\n';
  43.         }
  44.     }
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement