Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 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. int main()
  7. {
  8. int c[105], n, a[105][105]={0}, x, t[105]={0}, ic, sfc, d[105]={0}, i, j;
  9.  
  10. fin>>n>>x;
  11. while(fin>>i>>j)
  12. a[i][j]=a[j][i]=1;
  13.  
  14. ic=sfc=1;
  15. d[x]=1;
  16. c[1]=x;
  17. while(ic<=sfc)
  18. {
  19. int k=c[ic];
  20. for(int i=1; i<=n; i++)
  21. if(a[k][i]==1 && d[i]==0)
  22. {
  23. c[++sfc]=i;
  24. d[i]=d[k]+1;
  25. t[i]=k;
  26. }
  27. ic++;
  28. }
  29. t[x]=0;
  30. for(int i=1; i<=n; i++)
  31. fout<<t[i]<<' ';
  32. return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement