Pagini recente » Cod sursa (job #158583) | Cod sursa (job #17562) | Cod sursa (job #551254) | Cod sursa (job #2175719) | Cod sursa (job #1764390)
#include <fstream>
#include <cstdlib>
using namespace std;
unsigned int N, M, S;
unsigned int x, y;
unsigned int * A[100001];
unsigned int B[100001];
bool seen[100001];
unsigned int node;
unsigned int i, first, last;
int sol[100001];
int main ()
{
ifstream fin ("bfs.in");
fin >> N >> M >> S;
for (i=1; i<=N; i++)
{
A[i] = (unsigned int *) realloc (A[i], sizeof (unsigned int));
A[i][0] = 0;
}
for (i=1; i<=M; i++)
{
fin >> x >> y;
A[x][0]++;
A[x] = (unsigned int *) realloc (A[x], (A[x][0]+1) * sizeof (unsigned int));
A[x][A[x][0]] = y;
}
fin.close();
B[1] = S;
first = last = 1;
sol[S] = 1;
seen[S] = 1;
while (first <= last)
{
node = B[first];
first++;
for (i=1; i<=A[node][0]; i++)
if (!seen[A[node][i]])
{
seen[A[node][i]] = 1;
sol[A[node][i]] = sol[node] + 1;
last++;
B[last] = A[node][i];
}
}
for (i=1; i<=N; i++)
sol[i]--;
ofstream fout ("bfs.out");
for (i=1; i<=N; i++)
fout << sol[i] << ' ';
fout.close();
return 0;
}