Pagini recente » Cod sursa (job #1089864) | Cod sursa (job #1851238) | Cod sursa (job #2765664) | Cod sursa (job #821164) | Cod sursa (job #1842870)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#define Nmax 100009
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int n,m,s,x,y,sol,cost[Nmax];
bitset <Nmax> viz;
vector <int> G[Nmax],cc;
queue <int> q;
void Bfs(int node, vector<int>& v)
{
q.pop();
v.push_back(node);
for (auto x : G[node])
if (!viz[x])
{
q.push(x);
viz[x]=1;
cost[x]=cost[node]+1;
}
}
int main()
{
f>>n>>m>>s;
for (int i=1; i<=m; ++i)
{
f>>x>>y;
G[x].push_back(y);
}
q.push(s);
viz[s]=1;
while (!q.empty()) Bfs(q.front(), cc);
for (int i=1; i<=n; ++i) if (!viz[i]) cost[i]=-1;
for (int i=1; i<=n; ++i) g<<cost[i]<<' ';
g<<'\n';
//for (auto x : cc) g<<x<<' ';
//g<<'\n';
f.close();
g.close();
return 0;
}