Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- ifstream fin("arbore.in");
- ofstream fout("arbore.out");
- int n,m,j,i,viz[101],a[101][101],c[101],x,niv[101],t[101];
- void citire()
- {int i,j;
- fin>>n>>m;
- while(fin>>i>>j)
- {
- a[i][j]=1;
- a[j][i]=1;
- }
- }
- void bfs(int x)
- {int p,u,k,i;
- p=u=1;
- c[p]=x;
- viz[x]=1;
- t[x]=0;
- while(p<=u)
- { k=c[p];
- p++;
- for(i=1;i<=n;i++)
- if(a[k][i]==1&&viz[i]==0)
- { u++;
- c[u]=i;
- viz[i]=1;
- t[i]=k;
- //niv[i]=niv[k]+1;
- }
- }
- }
- void afisare()
- {
- int i;
- for(i=1;i<=n;i++)
- fout<<t[i]<<' ';
- }
- int main()
- { citire();
- bfs(m);
- afisare();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement