Pagini recente » Cod sursa (job #1444558) | Monitorul de evaluare | Cod sursa (job #465852) | Cod sursa (job #49029) | Cod sursa (job #2235985)
#include <bits/stdc++.h>
#define NMAX 100005
#define INF 0x3f3f3f3f
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int N, M, S;
int parc[NMAX], inq[NMAX];
vector <int> v[NMAX];
queue <int> q;
void bfs()
{
parc[S] = 0;
q.push(S);
while (!q.empty())
{
int nod = q.front();
q.pop();
//inq[nod] = 0;
for (vector <int> :: iterator it = v[nod].begin(); it != v[nod].end(); it++)
{
int nxt = *it;
if (parc[nod] + 1 < parc[nxt])
{
parc[nxt] = parc[nod] + 1;
//if (!inq[nxt])
//{
q.push(nxt);
// inq[nxt] = 1;
//}
}
}
}
}
void afisare()
{
for (int i = 1; i <= N; i++)
if (parc[i] == INF)
fout << "-1 ";
else fout << parc[i] << " ";
}
int main()
{
memset(parc, INF, sizeof(parc));
fin >> N >> M >> S;
for (int i = 1; i <= M; i++)
{
int x, y;
fin >> x >> y;
v[x].push_back(y);
}
parc[S] = 0;
bfs();
afisare();
return 0;
}