Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main()
- {
- int t=Int;
- while(t--){
- int n=Int,m=Int;//n=node numbers & m=edges
- vector<int>adj[n+1];
- for(int a=1;a<=m;a++){
- int b=Int,c=Int;
- if(b>c)swap(b,c);
- adj[b].pushv(c);
- }
- int distance[n+1]={0},color[n+1]={white};
- queue<int>bfs;
- bfs.push(1);
- color[1]=grey;
- while(!bfs.empty()){
- int frnt=bfs.front();
- for(int b=0;b<adj[frnt].size();b++){
- int u=frnt,v=adj[frnt][b];
- if(color[v] == white){
- bfs.push(v);
- color[v]=grey;
- distance[v]=distance[u]+1;
- }
- }
- bfs.pop();
- }
- //for(int x=1;x<=n;x++)cout<<"distance of "<<x<<" from 1 is "<<distance[x]<<endl;
- cout<<distance[n]<<endl;
- }
- r0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement