The code is giving segmentation fault
code-https://ide.codingblocks.com/s/341563
Code not working
vector dis(100005,0);
this is correct way to declare vector of size N with intial val 0
also you have to set visited to 0 every time
I did the necessary changes but the code still isn’t getting submitted. code-https://ide.codingblocks.com/s/341563
there are few more mistakes in your code
- at line no 26 to 33
if (vis[x] == 0)
{
// dis[x]=dis[src]+1;
dis[x] = dis[node] + 1;
// also you have to mark x as visited
vis[x] = 1;
q.push(x);
}
- also in main()
while print the ans you have to run loop from i=1 to i<=n
this is because p can be 1 or not 1
so handle accordingly
i have done all these changes in modifed Code attached below
you can see and if you have any doubt feel free to ask
Modified Code
Thank you for the solution!