Advertisement
Dennnhhhickk

Untitled

Jan 5th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <cmath>
  5. #include <climits>
  6.  
  7. using namespace std;
  8.  
  9. struct puperfunctionmycreateion{
  10. int data, prev, st, ans;
  11. };
  12.  
  13. int main()
  14. {
  15. puperfunctionmycreateion temp, temp1;
  16. int n, m, free = 0, v, u, h;
  17. /*vector <int>*/ int a[100000], head[100000], ans[100000], next[100000], dest[100000];
  18. queue <puperfunctionmycreateion> q;
  19. cin >> n >> m;
  20. /*head.reserve(1000000);
  21. a.reserve(1000000);
  22. ans.reserve(1000000);
  23. dest.reserve(1000000);
  24. next.reserve(1000000);
  25. head.resize(n);
  26. ans.resize(n);
  27. a.resize(n);*/
  28. for (int i = 1; i <= n; i++){
  29. next[i] = 0;
  30. ans[i] = INT_MAX - 20;
  31. cin >> a[i];
  32. if (a[i] != 0){
  33. temp.data = i;
  34. temp.st = i;
  35. temp.ans = 0;
  36. temp.prev = 0;
  37. q.push(temp);
  38. }
  39. }
  40. for (int i = 1; i <= m; i++){
  41. cin >> v >> u;
  42. free++;
  43. //dest.resize(free + 1);
  44. //next.resize(free + 1);
  45. dest[free] = u;
  46. next[free] = head[v];
  47. head[v] = free;
  48. free++;
  49. dest[free] = v;
  50. next[free] = head[u];
  51. head[u] = free;
  52. }
  53. while (!q.empty()){
  54. temp = q.front();
  55. h = head[temp.data];
  56. while (h){
  57. if (dest[h] != temp.prev)
  58. if (a[dest[h]] == 0){
  59. temp1.data = dest[h];
  60. temp1.st = temp.st;
  61. temp1.ans = temp.ans + 1;
  62. temp1.prev = temp.data;
  63. q.push(temp1);
  64. }
  65. else
  66. if (a[dest[h]] != a[temp.st])
  67. ans[dest[h]] = min(ans[dest[h]], temp.ans + 1);
  68. h = next[h];
  69. }
  70. q.pop();
  71. }
  72. int min2;
  73. min2 = ans[1];
  74. for (int i = 2; i <= n; i++)
  75. min2 = min(min2, ans[i]);
  76. cout << min2 << "\n";
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement