Cod sursa(job #2752865)
| Utilizator | Data | 19 mai 2021 20:50:53 | |
|---|---|---|---|
| Problema | BFS - Parcurgere in latime | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.5 kb |
#include <fstream>
#define Nmax 10005
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
long long a[Nmax][Nmax],v[Nmax],c[Nmax];
int main()
{ int n,m,x;
f>>n>>m>>x;
for(int p,q;m;--m) {f>>p>>q; a[p][q]=a[q][p]=1;}
int p=1,u=1;
v[x]=1;c[1]=x;
while(p<=u)
{ int k=c[p];
for(int j=1;j<=n;j++)
if(a[k][j]==1 && v[j]==0) {v[j]=1; c[++u]=j;}
p++;
}
for(int i=1;i<=u;i++) g<<c[i]<<" ";
g.close();
return 0;
}
