Cod sursa(job #851474)

Utilizator blechereyalBlecher Eyal blechereyal Data 9 ianuarie 2013 23:34:01
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb

#include <fstream>
#include <vector>
#define max 100000
using namespace std;
int N,M,S;

vector <int> lista[max];

int cost[max],C[max];

ifstream f("bfs.in");
ofstream g("bfs.out");


void BFS(int S)
{int l,i,j;

for (i=1;i<=N;i++)
cost[i]=-1; //toate nodurile nevizitate

l=1;
C[l]=S;
cost[l]=0;

for (i=1;i<=l;i++)
 for (j=0;j<=lista[C[l]].size();j++)
     if (cost[lista[C[i]][j]]==-1)
        {
        l++;
        C[l]=lista[C[i]][j];
        cost[C[l]]=cost[C[i]]+1;
        }
}


int main()
{int i,nod1,nod2;
f>>N>>M>>S;

for (i=1;i<=M;i++)
    {
    f>>nod1>>nod2;
    lista[nod1].push_back(nod2);
    }

BFS(S);

for (i=1;i<=N;i++)
g<<cost[i]<<" ";
g<<"\n";
return 0;
}