Mai intai trebuie sa te autentifici.
Cod sursa(job #3305350)
| Utilizator | Data | 31 iulie 2025 22:03:06 | |
|---|---|---|---|
| Problema | BFS - Parcurgere in latime | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.82 kb |
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("bfs.in");
ofstream fout ("bfs.out");
const int Nmax = 1e5+5;
vector<int> ad[Nmax];
int Dist[Nmax];
queue<int> q;
int n,m,s;
void bfs(int node);
int main()
{
int i,a,b;
fin>>n>>m>>s;
for (i=1;i<=m;i++)
{
fin>>a>>b;
ad[a].push_back(b);
}
bfs(s);
for (i=1;i<=n;i++)
{
fout<<Dist[i]<<" ";
}
return 0;
}
void bfs(int node)
{
int i;
for (i=1;i<=n;i++)
{
Dist[i]=-1;
}
Dist[node]=0;
q.push(node);
while (!q.empty())
{
int org = q.front();
q.pop();
for (auto it : ad[org])
{
if (Dist[it]==-1)
{
Dist[it]=Dist[org]+1;
q.push(it);
}
}
}
}