Mai intai trebuie sa te autentifici.
Cod sursa(job #2270637)
Utilizator | Data | 27 octombrie 2018 12:02:06 | |
---|---|---|---|
Problema | BFS - Parcurgere in latime | Scor | 0 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.83 kb |
#include <bits/stdc++.h>
#define nmax 100001
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector <int> matrice[nmax];
int viz[nmax],dist[nmax];
int nc;
deque <int>v;
int main()
{
int n,m,x,y,s;
f>>n>>m>>s;
for(int i=1;i<=m;i++){
f>>x>>y;
matrice[x].push_back(y);
}
v.push_back(s);
viz[s]=1;
int nod,sz;
memset(dist,-1,nmax);
dist[s]=0;
while(v.empty()==false){
nod=v[0];
sz=matrice[nod].size();
for(int i=0;i<sz;i++)
if(viz[matrice[nod][i]]==0){
v.push_back(matrice[nod][i]);
dist[matrice[nod][i]]=dist[nod]+1;
viz[matrice[nod][i]]=1;
}
v.pop_front();
}
for(int i=1;i<=n;i++)
g<<dist[i]<<" ";
return 0;
}