#include<bits/stdc++.h>
using namespace std;
class Graph
{
int v;
list *l;
public:
Graph(int v)
{
this->v=v;
l= new list[v+1];
}
void addedge(int x,int y)
{
l[x].push_back(y);
l[y].push_back(x);
}
void bfs(int s)
{
queue q;
int dist[v+1]={0};
bool visited[v+1]={false};
visited[s]=true;
q.push(s);
while(!q.empty())
{
int node=q.front();
for(auto nbr:l[node])
{
visited[nbr]=true;
q.push(nbr);
dist[nbr]=dist[node]+6;
}
q.pop();
}
for(int i=1;i<=v;i++)
if(i != s) {
if(dist[i] == 0) {
cout<<"-1 “;
} else {
cout<<dist[i]<<” ";
}
} }
};
int main()
{
int c,e,e1,e2;
cin>>c;
Graph g©;
cin>>e;
for(int i=0;i<e;i++)
{
cin>>e1>>e2;
g.addedge(e1,e2);
}
int src;
cin>>src;
g.bfs(src);
}
I m getting segmentation fault in this code
please send link of your code
it is very difficult to detect error here
because there is no indentation and also some special character has special meaning
from where should i share the link
hi @PoojaSingh22
your logic and code is correct one mistake which you have done
don’t take no of testcase
first line is no of testcase which you have not take into consideration
modified code
i hope this help
thanks for the help…but even for a single test case i was getting segmenatation fault…i was not submitting this code…i knew that i have not included the test case but it should work for atleast one input…and there only i was getting segmentation fault
No, it will work
but for that you have to remove first line from input
you can see modified code
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.