Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <vector>
  5. #define min(a,b) ((a<b) ? (a) : (b))
  6. #define max(a,b) ((a>b) ? (a) : (b))
  7. #define pb push_back
  8. #define mp make_pair
  9. #define ll long long
  10.  
  11. using namespace std;
  12.  
  13. int cauta_binar(int*v, int a, int b, int x);
  14. void fill(int x, int y);
  15. void DFS(int x);
  16. void BFS(int start);
  17.  
  18. int n, m, uz[1005], v[105], a[105][105];
  19. int dx[] = {-1, 0, 1, 0};
  20. int dy[] = { 0, 1, 0,-1};
  21. vector<int> L[1005];
  22.  
  23. int main()
  24. {
  25. int i, x, y;
  26.  
  27. cin >> n >> m;
  28. for (i = 1; i <= m; i++)
  29. {
  30. cin >> x >> y;
  31. L[x].pb(y);
  32. L[y].pb(x);
  33. }
  34. DFS(1);
  35. cout << '\n';
  36. memset(uz, 0, sizeof(uz));
  37. BFS(1);
  38. return 0;
  39. }
  40.  
  41. void DFS(int x)
  42. {
  43. int i;
  44.  
  45. uz[x] = 1;
  46. cout << x << ' ';
  47. for (i = 0; i < L[x].size(); i++)
  48. if (!uz[L[x][i]])
  49. DFS(L[x][i]);
  50. }
  51.  
  52. void BFS(int start)
  53. {
  54. int i, in, sf, aux, C[1005];
  55.  
  56. in = sf = 0;
  57. C[sf++] = start;
  58. uz[start] = 1;
  59. while (in < sf)
  60. {
  61. aux = C[in++];
  62. cout << aux << ' ';
  63. for (i = 0; i < L[aux].size(); i++)
  64. if (!uz[L[aux][i]])
  65. {
  66. uz[L[aux][i]] = 1;
  67. C[sf++] = L[aux][i];
  68. }
  69. }
  70. cout << '\n';
  71. }
  72.  
  73. void fill(int x, int y)
  74. {
  75. int i, l9, c9;
  76.  
  77. a[x][y] = 0;
  78. for (i = 0; i < 4; i++)
  79. {
  80. l9 = x + dx[i];
  81. c9 = y + dy[i];
  82. if (a[l9][c9] == 1)
  83. fill(l9, c9);
  84. }
  85. }
  86.  
  87. int cauta_binar(int*v, int a, int b, int x)//cauta in intervalul [a,b] din vector prima aparitie a lui x si o returneaza
  88. { //daca nu exista este returnata pozitia unde ar trebui inserat
  89. int in, sf, mij;
  90.  
  91. in = a - 1;
  92. sf = b + 1;
  93. while (sf - in > 1)
  94. {
  95. mij = (in + sf) / 2;
  96. if (v[mij] < x)
  97. in = mij;
  98. else sf = mij;
  99. }
  100. return sf;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement