Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 10010;
- vector<int> G[N];
- bool vis[N];
- int dist[N];
- int main()
- {
- int k, n, m;
- scanf("%d%d%d", &k, &n, &m);
- for (int i = 0; i < m; ++i) {
- int u, v;
- scanf("%d%d", &u, &v);
- G[u].push_back(v);
- }
- queue<int> Q;
- dist[1] = 0;
- vis[1] = true;
- Q.push(1);
- int mx = 1;
- while (!Q.empty()) {
- int u = Q.front();
- Q.pop();
- if (dist[u] > k)
- continue;
- mx = max(mx, u);
- for (auto v : G[u]) {
- if (!vis[v]) { // you can also check (dist[u]+1 <= k) if you want faster time
- dist[v] = dist[u]+1; // can also do mx = max(mx, v); here for faster time
- vis[v] = true;
- Q.push(v);
- }
- }
- }
- printf("%d\n", mx);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement