Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <stdio.h>
- using namespace std;
- vector<int> g[100010];
- bool visited[100010];
- int e[100010];
- int main(){
- int k , n ,m;
- scanf("%d%d%d",&k,&n,&m);
- int u ,v;
- for(int i=1;i<=m;++i){
- scanf("%d%d",&u,&v);
- g[u].push_back(v);
- }
- queue<int> q;
- q.push(1);
- e[1]=0;
- int max1=-1e9;
- for(int i=2;i<=n;++i){
- e[i]=10000000;
- }
- while(!q.empty()){
- int y=q.front();
- q.pop();
- if(max1<y){
- max1=y;
- }
- visited[y]=true;
- if(e[y]+1<=k){
- for(auto c: g[y]){
- if(!visited[c]){
- q.push(c);
- visited[c]=true;
- if(e[c]>e[y]+1){
- e[c]=e[y]+1;
- }
- }
- }
- }
- }
- cout << max1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement