#include<iostream>
#include<queue>
#define max 1000
using namespace std;
vector<int>graph[max];
void bfs(int node,int source)
{
int u,v,i;
queue<int>que;
int taken[max]={0};
taken[source]=1;
int distance[max];
distance[source]=0;
que.push(source);
while(!que.empty())
{
int u=que.front();
for(int i=0;i<graph[u].size();i++)
{
v=graph[u][i];
if(taken[v]==0)
{
distance[v]=distance[u]+1;
que.push(v);
taken[v]=1;
}
}
que.pop();
}
for(i=1;i<=node;i++)
{
cout<<"distance from source :: ";
cout<<distance[i]<<endl;
}
}
int main()
{
int node,edge,i,x,y,j;
cin>>node>>edge;
for(i=1;i<=edge;i++)
{
cin>>x>>y;
graph[x].push_back(y);
graph[y].push_back(x);
}
int source;
cin>>source;
bfs(node,source);
}
#include<queue>
#define max 1000
using namespace std;
vector<int>graph[max];
void bfs(int node,int source)
{
int u,v,i;
queue<int>que;
int taken[max]={0};
taken[source]=1;
int distance[max];
distance[source]=0;
que.push(source);
while(!que.empty())
{
int u=que.front();
for(int i=0;i<graph[u].size();i++)
{
v=graph[u][i];
if(taken[v]==0)
{
distance[v]=distance[u]+1;
que.push(v);
taken[v]=1;
}
}
que.pop();
}
for(i=1;i<=node;i++)
{
cout<<"distance from source :: ";
cout<<distance[i]<<endl;
}
}
int main()
{
int node,edge,i,x,y,j;
cin>>node>>edge;
for(i=1;i<=edge;i++)
{
cin>>x>>y;
graph[x].push_back(y);
graph[y].push_back(x);
}
int source;
cin>>source;
bfs(node,source);
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন