Mai intai trebuie sa te autentifici.
Cod sursa(job #2649705)
| Utilizator | Data | 15 septembrie 2020 21:54:45 | |
|---|---|---|---|
| Problema | BFS - Parcurgere in latime | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.99 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n, m, startnod;
vector <int> muchii[100100];
queue <int> coada;
int distanta[100100];
void bfs()
{
int nod;
while(!coada.empty())
{
nod = coada.front();
coada.pop();
for(unsigned int i = 0; i < muchii[nod].size(); i ++)
{
if(distanta[muchii[nod][i]] == -1)
{
coada.push(muchii[nod][i]);
distanta[muchii[nod][i]] = distanta[nod] + 1;
}
}
}
}
void citire()
{
fin >> n >> m >> startnod;
for(int i = 1; i <= m; i ++)
{
int x, y;
fin >> x >> y;
muchii[x].push_back(y);
}
for(int i = 1; i <= n; i ++)
{
distanta[i] = -1;
}
distanta[startnod] = 0;
coada.push(startnod);
bfs();
for(int i = 1; i <= n; i ++)
fout << distanta[i] << " ";
}
int main()
{
citire();
}
