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