Pagini recente » Cod sursa (job #2837392) | Cod sursa (job #2684774) | Cod sursa (job #927683) | Monitorul de evaluare | Cod sursa (job #793856)
Cod sursa(job #793856)
#include<fstream>
using namespace std;
const int NMAX = 100001;
const int MMAX = 1000001;
struct muchie {
int x,y;
};
int cost[NMAX],l[NMAX],c[NMAX],*v[NMAX],n;
muchie a[MMAX];
inline void bfs(int nod)
{
int i,st,dr;
for(i=1;i<=n;i++)
cost[i]=-1;
st=1;
dr=1;
c[st]=nod;
cost[nod]=0;
while(st<=dr) {
for(i=0;i<=l[c[st]];i++)
if(cost[v[c[st]][i]]==-1) {
cost[v[c[st]][i]]=cost[c[st]]+1;
c[++dr]=v[c[st]][i];
}
st++;
}
}
int main ()
{
int m,i,s;
ifstream f("bfs.in");
ofstream g("bfs.out");
f>>n>>m>>s;
for(i=1;i<=m;i++) {
f>>a[i].x>>a[i].y;
l[a[i].x]++;
}
f.close();
for(i=1;i<=n;i++) {
v[i]=new int[l[i]];
l[i]=-1;
}
for(i=1;i<=m;i++)
v[a[i].x][++l[a[i].x]]=a[i].y;
bfs(s);
for(i=1;i<=n;i++)
g<<cost[i]<<" ";
g.close();
return 0;
}