Pagini recente » Cod sursa (job #1819042) | Cod sursa (job #2750271) | Cod sursa (job #2700671) | Cod sursa (job #3203767) | Cod sursa (job #2418233)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
//ifstream fin("bfs.in");
//ofstream fout("bfs.out");
FILE *fin=fopen("bfs.in","r");
FILE *fout=fopen("bfs.out","w");
const int nmax=100005;
char buff[nmax];
vector <int> g[nmax];
vector <int> viz(nmax,-1);
int n,m,s,poz;
inline void read(int &nr)
{
while(buff[poz]<'0' || buff[poz]>'9')
if(++poz==nmax)
fread(buff,1,nmax,fin),poz=0;
while(buff[poz]>='0' && buff[poz]<='9')
{
nr=nr*10+buff[poz]-'0';
if(++poz==nmax)
fread(buff,1,nmax,fin),poz=0;
}
}
void BFS(int s)
{
queue <int> q;
q.push(s);
viz[s]=0;
while(!q.empty())
{
int v=q.front();
for(unsigned int i=0; i<g[v].size(); i++)
if(viz[g[v][i]]==-1)
{
viz[g[v][i]]=viz[v]+1;
q.push(g[v][i]);
}
q.pop();
}
}
int main()
{
read(n);
read(m);
read(s);
for(int i=1; i<=m; i++)
{
int x=0,y=0;
read(x);
read(y);
g[x].push_back(y);
}
BFS(s);
for(int i=0; i<n; i++)
fprintf(fout,"%d ",viz[i+1]);
}