Pagini recente » Profil TheBatman | Cod sursa (job #676845) | Solutii .com 2009, Runda 2 | Monitorul de evaluare | Cod sursa (job #2783990)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
std::ifstream in("bfs.in");
std::ofstream out("bfs.out");
const int N=100000;
std::vector<int> v[N+1];
bool visited[N+1];
int cost[N+1];
std::queue<int> q;
void Bfs()
{
while(!q.empty())
{
int curent=q.front();
std::cout<<curent<<" ";
for(int i : v[curent])
{
if(!visited[i])
{
cost[i]=cost[curent]+1;
q.push(i);
}
}
visited[curent]=1;
q.pop();
}
}
int main()
{
int n, m, s, x, y;
in>>n>>m>>s;
for(int i=1; i<=m; i++)
{
in>>x>>y;
if(x!=y) v[x].push_back(y);
}
q.push(s);
Bfs();
for(int i=1; i<=n; i++)
{
if(i==s) out<<0<<" ";
else
{
if(cost[i]==0)
{
out<<-1<<" ";
}
else
{
out<<cost[i]<<" ";
}
}
}
}