Advertisement
Dennnhhhickk

Untitled

Jan 6th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 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. int find228(queue <puperfunctionmycreateion>, int);
  13. int find1337(queue <puperfunctionmycreateion>, int);
  14.  
  15. int main()
  16. {
  17. puperfunctionmycreateion temp, temp1;
  18. int n, m, free = 0, v, u, h;
  19. vector <int> a, head, ans, next, dest, mark, ans1;
  20. queue <puperfunctionmycreateion> q;
  21. cin >> n >> m;
  22. mark.reserve(100000);
  23. head.reserve(100000);
  24. a.reserve(100000);
  25. ans.reserve(100000);
  26. dest.reserve(100000);
  27. next.reserve(100000);
  28. head.resize(n);
  29. ans.resize(n);
  30. a.resize(n);
  31. mark.resize(n);
  32. next.resize(n);
  33. for (int i = 1; i <= n; i++) {
  34. next[i] = 0;
  35. mark[i] = 0;
  36. ans[i] = INT_MAX - 20;
  37. mark[i] = 0;
  38. cin >> a[i];
  39. if (a[i] != 0) {
  40. temp.data = i;
  41. temp.st = i;
  42. temp.ans = 0;
  43. temp.prev = 0;
  44. q.push(temp);
  45. mark[i] = 1;
  46. }
  47. }
  48. for (int i = 1; i <= m; i++) {
  49. cin >> v >> u;
  50. free++;
  51. dest.resize(free + 3);
  52. next.resize(free + 3);
  53. dest[free] = u;
  54. next[free] = head[v];
  55. head[v] = free;
  56. free++;
  57. dest[free] = v;
  58. next[free] = head[u];
  59. head[u] = free;
  60. }
  61. while (!q.empty()) {
  62. temp = q.front();
  63. q.pop();
  64. h = head[temp.data];
  65. while (h) {
  66. if (dest[h] != temp.prev)
  67. if (!mark[dest[h]]) {
  68. temp1.data = dest[h];
  69. temp1.st = temp.st;
  70. temp1.ans = temp.ans + 1;
  71. temp1.prev = temp.data;
  72. q.push(temp1);
  73. mark[dest[h]] = 1;
  74. }
  75. else
  76. if (a[temp.st] != a[find1337(q, dest[h])])
  77. if (a[dest[h]] == 0)
  78. ans[temp.st] = temp.st + find228(q, dest[h]);
  79. else
  80. ans[dest[h]] = temp.ans + 1;
  81. h = next[h];
  82. }
  83. }
  84. int min2;
  85. min2 = ans[1];
  86. for (int i = 1; i <= n; i++)
  87. //min2 = min(min2, ans[i]);
  88. cout << ans[i] << endl;
  89. cout << min2 << "\n";
  90. return 0;
  91. }
  92.  
  93. int find228(queue <puperfunctionmycreateion> f, int data1) {
  94. int ans1;
  95. while (!f.empty()) {
  96. if (f.front().data == data1)
  97. ans1 = f.front().ans;
  98. f.pop();
  99. }
  100. return ans1;
  101. }
  102.  
  103. int find1337(queue <puperfunctionmycreateion> f, int data1) {
  104. int ans1;
  105. while (!f.empty()) {
  106. if (f.front().data == data1)
  107. ans1 = f.front().st;
  108. f.pop();
  109. }
  110. return ans1;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement