Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main()
- {
- #ifndef ONLINE_JUDGE
- //freopen("cin.txt","r",stdin);
- //freopen("cout.txt","w",stdout);
- #endif // ONLINE_JUDGE
- //speed
- int t=Int;
- while(t--){
- int n=Int,e=Int;
- vector<int> adj[n+1];
- for(int a=1;a<=e;a++){
- int x=Int,y=Int;
- if(x>y)swap(x,y);
- adj[x].pb(y);
- }
- stack<int> dfs;
- dfs.push(1);
- int distance[n+1]={0};
- int color[n+1]={0};
- while(!dfs.empty()){
- int top=dfs.top();
- bool found=false;
- for(int a=0;a<adj[top].size();a++){
- int u=top,v=adj[top][a];
- if(color[v]==0){
- dfs.push(v);
- distance[v]=distance[u]+1;
- color[v]=1;
- found=true;
- }
- }
- if(!found){
- dfs.pop();
- }
- }
- cout<<distance[n]<<endl;
- }
- r0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement