Mai intai trebuie sa te autentifici.
Cod sursa(job #2783603)
Utilizator | Data | 14 octombrie 2021 19:25:00 | |
---|---|---|---|
Problema | BFS - Parcurgere in latime | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.83 kb |
#include <fstream>
#include <queue>
#include <vector>
#define N 100001
using namespace std;
ifstream in ("bfs.in");
ofstream out ("bfs.out");
vector<int> graph[N];
int d[N];
queue<int> q;
void bfs()
{
while(!q.empty())
{
int x= q.front();
q.pop();
for(auto y: graph[x])
{
if(d[y]==-1)
{
d[y]=1+d[x];
q.push(y);
}
}
}
}
int main()
{
int n,m,s,x,y;
in >> n >> m >> s;
for(int i=1; i<=n; i++)
{
d[i]=-1;
}
for(int i=0; i<m; i++)
{
in >> x >> y;
graph[x].push_back(y);
//graph[y].push_back(x);
}
q.push(s);
d[s]=0;
bfs();
for(int i=1; i<=n; i++)
out << d[i] << " ";
return 0;
}