Pagini recente » Cod sursa (job #826235) | Cod sursa (job #537250) | Cod sursa (job #400683) | Cod sursa (job #2708653) | Cod sursa (job #736164)
Cod sursa(job #736164)
#include<fstream>
#include<stdlib.h>
#define nmax 100002
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
int n,m ,*A[nmax],i,j, s;
int c[1000000];
int v[nmax];
bool viz[nmax];
void dfs(int x)
{
int i,j ;
int p , u = 1;
p = 1;
c[1] = x ;
viz[x] =true ;
while(p <= u)
{
x = c[p];
for(i = 1 ; i <= A[x][0] ; i++ )
{
if( viz[ A[x][i] ] == false )
{
v[ A[x][i] ] = v[x] + 1 ;
viz[A[x][i]] = true ;
c[++u] = A[x][i] ;
}
}
++p;
}
}
void read()
{
int i ,j ,x, y;
fin >> n >> m >>s;
for(i = 1 ;i <= n; i++)
{
A[i] = (int *) realloc(A[i], sizeof(int));
A[i][0] = 0 ;
}
for(i = 1 ;i <= m ; i++)
{
fin >>x >> y;
A[x][0] ++ ;
A[x] = (int *) realloc(A[x], ( A[x][0] + 1 ) * sizeof(int));
A[x][A[x][0]]= y;
}
dfs(s);
for(i = 1; i <= n; i++)
if(v[i] == 0 && s!= i )
fout << -1 <<" " ;
else
fout <<v[i] <<" ";
}
int main()
{
read();
fin.close();
fout.close();
return 0;
}