Pagini recente » Cod sursa (job #184668) | Cod sursa (job #2217789) | Calibrare limite de timp | Istoria paginii runda/oni_2004 | Cod sursa (job #2924811)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("graf.in");
ofstream fout("graf.out");
vector <int> *la;
void BFS(int s, int viz[], int dist[])
{
queue<int> C;
C.push(s);
viz[s]=1;
dist[s]=0;
while(C.size()>0)
{
int u=C.front();
C.pop();
for (int i=0; i<la[u].size(); i++)
{int y=la[u][i];
if(viz[y]==0)
{
viz[y]=1;
dist[y]=dist[u]+1;
C.push(y);
}}
}
}
int main()
{
int n, m, s, x, y;
fin>>n>>m>>s;
la= new vector<int> [n+1];
int c=m;
while (c>0)
{
fin>>x>>y;
la[x].push_back(y);
//la[y].push_back(x);
c--;
}
/*
for(x=1; x<=n; x++)
{
cout<<x<<": ";
for (int i=0; i<la[x].size(); i++)
cout<<la[x][i]<<" ";
cout<<endl;
}
*/
int viz[n+1]={}, dist[n+1];
for(int j=1; j<=n; j++)
dist[j]=-1;
BFS(s, viz, dist);
for(int j=1; j<=n; j++)
fout<<dist[j]<<" ";
return 0;
}
/*
9 11
1 2
1 3
1 4
2 5
2 9
3 5
3 7
5 7
6 7
6 8
4 6
*/