Mai intai trebuie sa te autentifici.
Cod sursa(job #1650852)
Utilizator | Data | 11 martie 2016 20:59:28 | |
---|---|---|---|
Problema | BFS - Parcurgere in latime | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.07 kb |
#include <fstream>
#include <vector>
#include <deque>
#define u_int unsigned int
using namespace std;
vector<u_int> L[100000 + 1];
vector<int> cost(100000 + 1, -1);
vector<u_int> tata(100000 + 1, 0);
void bfs(int nod)
{
deque<u_int> coada;
cost[nod] = 0;
coada.push_back(nod);
tata[nod] = nod;
while(coada.empty() == false)
{
for(u_int j = 0; j < L[coada.front()].size(); j++)
{
if(cost[L[coada.front()][j]] == -1)
{
coada.push_back(L[coada.front()][j]);
cost[coada.back()] = cost[coada.front()] + 1;
tata[coada.back()] = coada.front();
}
}
coada.pop_front();
}
}
int main()
{
ifstream fin("bfs.in");
ofstream fout("bfs.out");
u_int n, m, s;
fin >> n >> m >> s;
for(u_int i = 1; i <= m; i++)
{
u_int x, y;
fin >> x >>y;
L[x].push_back(y);
}
bfs(s);
for(u_int i = 1; i <= n; i++)
{
fout << cost[i] << ' ';
}
return 0;
}