Mai intai trebuie sa te autentifici.
Cod sursa(job #2116682)
| Utilizator | Data | 27 ianuarie 2018 20:47:24 | |
|---|---|---|---|
| Problema | BFS - Parcurgere in latime | Scor | 70 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.65 kb |
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define NM 100010
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int n, m, s, x, y;
int dist[100000];
vector <int> v[NM];
queue <int> q;
void bfs()
{
q.push(s);
dist[s] = 1;
while (!q.empty())
{
for (auto i : v[q.front()])
{
if (dist[i] == 0)
{
dist[i] = dist[q.front()] + 1;
q.push(i);
}
}
q.pop();
}
for (int i = 1; i <= n; i++)
g << dist[i] - 1 << " ";
}
int main()
{
f >> n >> m >> s;
for (int i = 1; i <= m; i++)
{
f >> x >> y;
v[x].push_back(y);
}
bfs();
return 0;
}
