Advertisement
AquaBlitz11

programming.in.th - 1031

Jan 17th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 10010;
  5.  
  6. vector<int> G[N];
  7. bool vis[N];
  8. int dist[N];
  9.  
  10. int main()
  11. {
  12.     int k, n, m;
  13.     scanf("%d%d%d", &k, &n, &m);
  14.     for (int i = 0; i < m; ++i) {
  15.         int u, v;
  16.         scanf("%d%d", &u, &v);
  17.         G[u].push_back(v);
  18.     }
  19.     queue<int> Q;
  20.     dist[1] = 0;
  21.     vis[1] = true;
  22.     Q.push(1);
  23.     int mx = 1;
  24.     while (!Q.empty()) {
  25.         int u = Q.front();
  26.         Q.pop();
  27.         if (dist[u] > k)
  28.             continue;
  29.         mx = max(mx, u);
  30.         for (auto v : G[u]) {
  31.             if (!vis[v]) { // you can also check (dist[u]+1 <= k) if you want faster time
  32.                 dist[v] = dist[u]+1; // can also do mx = max(mx, v); here for faster time
  33.                 vis[v] = true;
  34.                 Q.push(v);
  35.             }
  36.         }
  37.     }
  38.     printf("%d\n", mx);
  39.    
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement