Cod sursa(job #349942)

Utilizator georgelRector George georgel Data 21 septembrie 2009 21:31:49
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include<fstream>
#define Max 20000

using namespace std;

ifstream fin("bfs.in");
ofstream fout("bfs.out");

int n,m,s,a[Max][Max],b[Max],c[Max],pus[Max];
void read(){
     int x,y;
     fin>>n>>m>>s;
     for(int i = 1; i <= m; i++)
     {
             fin>>x>>y;
             a[x][0]++;
             a[x][a[x][0]] = y;
     }
fin.close();
}
int gasit(int lin,int nod){
    for(int i = 1; i <= a[lin][0]; i++)
    if(a[lin][i] == nod)
    return 1;
return 0;
}
void bf(){
     int st,dr,i;
     st = 1;
     dr = 1;
     while(st <= dr){
              for(i = 1; i <= n;i++)
              if(!pus[i] && gasit(b[st],i) == 1)
              {
                         pus[i] = 1;
                         dr++;
                         b[dr] = i;
                         c[i] = c[b[st]]+1;
                         
              }
     st++;
     }
}
int main(){
    read();
    pus[s] = 1;
    b[1] = s;
    bf();
for(int i = 1; i <= n; i++)
if(pus[i] == 0)
c[i] = -1;
for(int i = 1; i <= n; i++)
fout<<c[i]<<" ";
fout.close();
return 0;
}