Pagini recente » Cod sursa (job #628833) | Cod sursa (job #1536712) | Cod sursa (job #930950) | Cod sursa (job #860943) | Cod sursa (job #1890695)
#include <fstream>
long a[100001][64], d[100001], n, i, j, q;
void read()
{
std::ifstream f("bfs.in");
long x, y, m;
f >> n >> m >> q;
for (i = 1; i <= n; i++)
d[i] = -1, a[i][0] = 0;
d[q] = 0;
for (i = 1; i <= m; i++)
{
f >> x >> y;
if (x != y)
a[x][ ++a[x][0] ] = y;
}
f.close();
}
void solve()
{
long x[100001], c;
x[ x[0] = 1 ] = q;
for (i = 1; i <= x[0]; i++)
for (j = 1; j <= a[ x[i] ][0]; j++)
{
c = a[ x[i] ][j];
if (d[c] == -1)
{
d[c] = d[ x[i] ] + 1;
x[ ++x[0] ] = c;
}
}
}
void write()
{
std::ofstream f("bfs.out");
for (i = 1; i <= n; i++)
f << d[i] << ' ';
f.close();
}
int main()
{
read();
solve();
write();
return 0;
}