tanasaradu

Untitled

Dec 14th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define nmax 101
  3. using namespace std;
  4. ifstream fin("dmax.in");
  5. ofstream fout("dmax.out");
  6. int n,m,d[nmax],viz[nmax];
  7. vector<int>h[nmax];
  8. queue<int>c;
  9. void Citire()
  10. {
  11. int i,x,y;
  12. fin>>n>>m;
  13. for(i=1;i<=m;i++)
  14. {
  15. fin>>x>>y;
  16. h[x].push_back(y);
  17. h[y].push_back(x);
  18. }
  19. }
  20. void BFS()
  21. {
  22. int i,x,p;
  23. p=1;
  24. viz[p]=1;
  25. c.push(p);
  26. while(!c.empty())
  27. {
  28. x=c.front();
  29. c.pop();
  30. for(i=0;i<h[x].size();i++)
  31. if(!viz[h[x][i]])
  32. {
  33. viz[h[x][i]]=1;
  34. c.push(h[x][i]);
  35. d[h[x][i]]=d[x]+1;
  36. }
  37. }
  38.  
  39. }
  40. int main()
  41. {
  42. int i,maxim=0,poz;
  43. Citire();
  44. BFS();
  45. for(i=1;i<=n;i++)
  46. if(maxim<d[i])
  47. {
  48. maxim=d[i];
  49. poz=i;
  50. }
  51. fout<<poz<<" ";
  52. fin.close();
  53. fout.close();
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment