Pagini recente » Cod sursa (job #211516) | Cod sursa (job #259483) | Cod sursa (job #234660) | Clasament concursul_lui_mati | Cod sursa (job #1813420)
#include<bits/stdc++.h>
#include<vector>
using namespace std;
#define NMax 100010
vector<int> a[NMax];
queue <int> q;
int M, N, Start;
int Cost[NMax];
bool viz[NMax];
void bfs(int x)
{
for(int i = 0; i < a[x].size(); ++i)
{
if(!viz[a[x][i]])
{
q.push(a[x][i]);
Cost[a[x][i]] = Cost[x] + 1;
viz[a[x][i]] = 1;
}
}
q.pop();
if(!q.empty())
bfs(q.front());
}
int main()
{
ifstream f("bfs.in");
ofstream g("bfs.out");
f>>N>>M>>Start;
memset(viz, false, sizeof(viz));
q.push(Start);
viz[Start] = 1;
int x, y;
for(int i = 1; i <= M; i ++)
{
f>>x>>y;
a[x].push_back(y);
}
bfs(Start);
for (int i = 1; i <= N; i++){
if(Cost[i] == 0 && i != Start)
g<<-1<<" ";
else
g<<Cost[i]<<" ";
}
return 0;
}