Advertisement
Dennnhhhickk

Untitled

Jan 6th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 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. bool bol;
  12. };
  13. int find228(queue <puperfunctionmycreateion>, int);
  14. int find1337(queue <puperfunctionmycreateion>, int);
  15. int view(vector <int>);
  16. puperfunctionmycreateion check(queue <puperfunctionmycreateion>, int);
  17.  
  18. int main()
  19. {
  20. puperfunctionmycreateion temp, temp1, pituh2281337332;
  21. int n, m, free = 0, v, u, h, data1;
  22. vector <int> a, ans, mark, ans1;
  23. vector <vector <int> > dp;
  24. queue <puperfunctionmycreateion> q;
  25. cin >> n >> m;
  26. mark.reserve(n + 1);
  27. a.reserve(n + 1);
  28. ans.reserve(n + 1);
  29. ans.resize(n + 1);
  30. a.resize(n + 1);
  31. mark.resize(n + 1);
  32. dp.reserve(n + 1);
  33. dp.resize(n + 1);
  34. for (int i = 1; i <= n; i++){
  35. mark[i] = 0;
  36. ans[i] = INT_MAX - 20;
  37. cin >> a[i];
  38. if (a[i] != 0) {
  39. temp.data = i;
  40. temp.st = i;
  41. temp.ans = 0;
  42. temp.prev = 0;
  43. q.push(temp);
  44. mark[i] = 1;
  45. }
  46. }
  47. for (int i = 1; i <= n; i++) {
  48. cin >> v >> u;
  49. dp[v - 1].push_back(u);
  50. dp[u - 1].push_back(v);
  51. }
  52. while (!q.empty()) {
  53. temp = q.front();
  54. q.pop();
  55. for (int i = 0; i < dp[temp.data - 1].size(); i++){
  56. data1 = dp[temp.data - 1][i];
  57. if (data1 != temp.prev)
  58. if (!mark[data1]) {
  59. temp1.data = data1;
  60. temp1.st = temp.st;
  61. temp1.ans = temp.ans + 1;
  62. temp1.prev = temp.data;
  63. q.push(temp1);
  64. mark[data1] = 1;
  65. }
  66. else {
  67. pituh2281337332 = check(q, data1);
  68. if (pituh2281337332.bol)
  69. if (a[temp.st] != a[pituh2281337332.st])
  70. if (a[data1] == 0){
  71. ans[temp.st] = min(ans[temp.st], temp.ans + pituh2281337332.ans + 1);
  72. ans[pituh2281337332.st] = min(ans[pituh2281337332.st], ans[temp.st]);
  73. }
  74. else{
  75. ans[data1] = min(ans[data1], temp.ans + 1);
  76. ans[pituh2281337332.st] = min(ans[pituh2281337332.st], ans[temp.st]);
  77. }
  78. }
  79. }
  80. }
  81. int min2;
  82. min2 = ans[1];
  83. for (int i = 2; i <= n; i++)
  84. min2 = min(min2, ans[i]);
  85. cout << min2 << "\n";
  86. return 0;
  87. }
  88.  
  89. puperfunctionmycreateion check(queue <puperfunctionmycreateion> f, int data1){
  90. int st, ans1 = -1, ans2;
  91. puperfunctionmycreateion ans5;
  92. while (!f.empty()) {
  93. if (f.front().data == data1){
  94. ans1 = f.front().data;
  95. st = f.front().st;
  96. ans2 = f.front().ans;
  97. }
  98. f.pop();
  99. }
  100. ans5.st = st;
  101. ans5.bol = (ans1 != -1);
  102. ans5.ans = ans2;
  103. return ans5;
  104. }
  105.  
  106. int view(vector <int> b) {
  107. for (int i = 1; i <= b.size(); i++)
  108. cout << b[i] << endl;
  109. cout << "end\n";
  110. return 0;
  111. }
  112.  
  113. /*
  114. 16 24
  115. 2 0 0 0 0 0 0 4 0 1 0 0 0 0 0 0
  116. 1 2
  117. 1 3
  118. 1 4
  119. 1 7
  120. 2 4
  121. 2 7
  122. 2 5
  123. 2 6
  124. 3 4
  125. 4 7
  126. 5 8
  127. 5 9
  128. 5 10
  129. 5 11
  130. 6 12
  131. 6 13
  132. 6 14
  133. 6 15
  134. 6 16
  135. 12 5
  136. 13 5
  137. 14 5
  138. 15 5
  139. 16 5*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement