Pagini recente » Istoria paginii runda/qweqwe/clasament | Cod sursa (job #1561042) | Cod sursa (job #1716394) | Cod sursa (job #2832530) | Cod sursa (job #2348150)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
void bfs(int nod, int distanta[], vector <int> graf[])
{
int n = graf[nod].size();
vector <int> u;
for (int i = 0; i < n; i++)
{
distanta[graf[nod][i]]++;
int m = graf[graf[nod][i]].size();
for (int j = 0; j < m; j++)
u.push_back(graf[graf[nod][i]][j]);
}
n = u.size();
for (int i = 0; i < n; i++)
bfs(u[i], distanta, graf);
}
int main()
{
int n, m, S;
f>>n>>m>>S;
vector <int> graf[n+1];
int x, y;
for (int i = 1; i <= m; i++)
{
f>>x>>y;
graf[x].push_back(y);
}
int distanta[n+1] = {0};
bfs(S, distanta, graf);
for (int i = 0; i < n; i++)
if (i != S)
g<<distanta[i]<<" ";
return 0;
}