Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. ifstream fin("arbore.in");
  5. ofstream fout("arbore.out");
  6.  
  7. void lee(int n,int a[100][100], int d[100],int k,int t[100])
  8. {
  9. int c[100],ic=1,sfc=1;
  10. c[1]=k;d[k]=1;
  11. while(ic<=sfc)
  12. {
  13.  
  14. int z=c[ic];
  15. for(int i=1; i<=n; i++)
  16. if(a[z][i] && d[i]==0)
  17. {
  18. c[++sfc]=i;
  19. d[i]=d[z]+1;
  20. t[i]=z;
  21. }
  22. ic++;
  23. }
  24. }
  25. int main()
  26. {
  27. int n,i,m,j,k,t[100]={0},a[100][100]= {0},d[100];
  28. fin>>n>>k;
  29. m=n-1;
  30. while(m)
  31. {
  32. fin>>i>>j;
  33. a[i][j]=1;
  34. a[j][i]=1;
  35. m--;
  36. }
  37.  
  38. lee(n,a,d,k,t);
  39. for(i=1; i<=n; i++)
  40. fout<<t[i]<<' ';
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement