Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <cstdio>
  3. #include <vector>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <map>
  8. #include <iterator>
  9. #include <functional>
  10. #include <set>
  11. #include <stack>
  12. #include <queue>
  13. #include <deque>
  14. #include <fstream>
  15. #include <iomanip>
  16. #include <numeric>
  17. #include <cmath>
  18. #include <list>
  19. #include <sstream>
  20. #include <cstring>
  21. #include <stdio.h>
  22. using namespace std;
  23. #pragma GCC optimize("O3")
  24. #pragma GCC target("sse4")
  25.  
  26. typedef long double LD;
  27. typedef long long LL;
  28. typedef unsigned long long ULL;
  29. typedef pair<int, int> PII;
  30. typedef pair<LD, LD> PDD;
  31. typedef pair<LL, int> PLL;
  32. typedef vector<int> VI;
  33. typedef vector<LL> VLL;
  34. typedef vector<char> VCH;
  35. typedef vector<LD> VLD;
  36. typedef vector<string> VS;
  37. typedef vector<VS> VSS;
  38. typedef vector<VI> VVI;
  39. typedef vector<VLL> VVLL;
  40. typedef vector<VCH> VVCH;
  41. typedef vector<PII> VPII;
  42. typedef vector<PLL> VPLL;
  43. typedef vector<PDD> VPDD;
  44. #define MP make_pair
  45. #define PB push_back
  46. #define X first
  47. #define Y second
  48. #define next fake_next
  49. #define prev fake_prev
  50. #define left fake_left
  51. #define right fake_right
  52.  
  53. #define FOR(i,a,b) for(int i = (a); i < (b); ++i)
  54. #define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); --i)
  55. #define REP(i, t) FOR(i,0,t)
  56. #define ALL(a) a.begin(), a.end()
  57. #define SZ(a) (int)((a).size())
  58. #define FILL(a, value) memset(a, value, sizeof(a))
  59.  
  60. const LD PI = acos(-1.0);
  61. const LD EPS = 1e-4;
  62. const LL INF = 1e7 - 1;
  63. const LL mod = 1000000007;
  64. const LL LINF = 1e18 + 10;
  65. const int MAXN = 10001;
  66. const int MAXK = 101;
  67.  
  68. int compar(string x, string y)
  69. {
  70. int mi = min(SZ(x), SZ(y));
  71. FOR(i, 0, mi)
  72. if (x[i] > y[i])
  73. return 0;
  74. else
  75. if (x[i] < y[i])
  76. return 1;
  77.  
  78. return SZ(x) < SZ(y);
  79. }
  80.  
  81. struct project
  82. {
  83. string name;
  84. int version;
  85. int index;
  86. VI sons;
  87. vector<project> children;
  88. };
  89.  
  90. bool operator<(const project& p, const project& r)
  91. {
  92. return compar(p.name, r.name);
  93. }
  94.  
  95. int n;
  96. vector<project> a;
  97. map<pair<string, int>, int> mp;
  98. map<string, vector<project>> names;
  99. VCH used;
  100. VI dist;
  101. vector<string> ans;
  102. vector<project> res;
  103.  
  104. void bfs()
  105. {
  106. queue<int> q;
  107. q.push(0);
  108. used.assign(n, 0);
  109. dist.assign(n, 0);
  110. used[0] = 1;
  111. while (!q.empty())
  112. {
  113. int t = q.front();
  114. q.pop();
  115. for(auto i: a[t].sons)
  116. if (used[i] == 0)
  117. {
  118. used[i] = 1;
  119. dist[i] = 1 + dist[t];
  120. q.push(i);
  121. }
  122. }
  123. }
  124.  
  125. int main()
  126. {
  127. ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  128. //freopen("In.txt", "r", stdin);
  129.  
  130. a.clear();
  131. cin >> n;
  132. string suka;
  133. FOR(i, 0, n)
  134. {
  135. //в рот їбав ссаний ввід
  136. //хуй його зна скільки вже пишу, а воно видає хуйню їбану якусь
  137. }
  138.  
  139. FOR(i, 0, n)
  140. {
  141. for (auto j : a[i].children)
  142. {
  143. auto wtf = MP(j.name, j.version);
  144. a[i].sons.push_back(mp[wtf]);
  145. }
  146. }
  147.  
  148. FOR(i, 0, n)
  149. names[a[i].name].push_back(a[i]);
  150.  
  151. bfs();
  152.  
  153. cout << SZ(names) << endl;
  154. for (map<string, vector<project>>::iterator it = names.begin(); it != names.end(); ++it)
  155. {
  156. auto i = *it;
  157. auto vec = i.Y;
  158. for(auto j: vec)
  159. if (used[j.index] == 1)
  160. {
  161. ans.push_back(i.X);
  162. break;
  163. }
  164. }
  165.  
  166. for (auto i : ans)
  167. {
  168. auto vec = names[i];
  169. project best;
  170. for(auto j: vec)
  171. if (used[j.index])
  172. {
  173. best = j;
  174. break;
  175. }
  176.  
  177. for(auto j: vec)
  178. if (used[j.index])
  179. {
  180. if (dist[j.index] < dist[best.index])
  181. best = j;
  182. else
  183. {
  184. if (dist[j.index] == dist[best.index])
  185. {
  186. if (j.version > best.version)
  187. best = j;
  188. }
  189. }
  190. }
  191.  
  192. res.push_back(best);
  193. }
  194.  
  195. cout << SZ(res) << endl;
  196. sort(ALL(res));
  197. for (auto i : res)
  198. cout << i.name << " " << i.version << endl;
  199.  
  200. cin >> n;
  201. return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement