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 main()
- {
- int c[105], n, a[105][105]={0}, x, t[105]={0}, ic, sfc, d[105]={0}, i, j;
- fin>>n>>x;
- while(fin>>i>>j)
- a[i][j]=a[j][i]=1;
- ic=sfc=1;
- d[x]=1;
- c[1]=x;
- while(ic<=sfc)
- {
- int k=c[ic];
- for(int i=1; i<=n; i++)
- if(a[k][i]==1 && d[i]==0)
- {
- c[++sfc]=i;
- d[i]=d[k]+1;
- t[i]=k;
- }
- ic++;
- }
- t[x]=0;
- for(int i=1; i<=n; i++)
- fout<<t[i]<<' ';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement