Advertisement
MegaVerkruzo

Untitled

Nov 9th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <algorithm>
  5. #include <deque>
  6. #include <cstdio>
  7.  
  8. using namespace std;
  9.  
  10. const int INF = 2e9;
  11.  
  12. int main()
  13. {
  14. freopen("input.txt", "r", stdin);
  15. int n, m, t;
  16. cin >> n >> m >> t;
  17. vector<vector<pair<int, char>>> g(n + 1);
  18. vector<pair<int, char>> obr(n + 1);
  19. for (int i = 2; i <= n; ++i) {
  20. int x;
  21. char c;
  22. cin >> x >> c;
  23. g[x].push_back({ i, c });
  24. obr[i].first = x;
  25. obr[i].second = c;
  26. }
  27. vector<vector<int>> heig;
  28. heig.reserve(1000);
  29. for (int i = 1; i <= n; ++i) {
  30. if (g[i].size() == 0) {
  31. int h = 0;
  32. int now = i;
  33. while (now == 1) {
  34. now = obr[now].first;
  35. h++;
  36. }
  37. heig.resize(h + 1);
  38. heig[h].push_back(i);
  39. }
  40. }
  41. vector<pair<string, int>> mer(m);
  42. for (int i = 0; i < m; ++i) {
  43. int x;
  44. string s;
  45. cin >> x >> s;
  46. mer[i].first = s;
  47. mer[i].second = x;
  48. }
  49. sort(mer.begin(), mer.end());
  50. vector<vector<int>> dp(n + 1, vector<int>(n + 1, INF));
  51. vector<int> start;
  52. for (int i = heig.size() - 1; i > 0; --i) {
  53. for (int l = 0; l < heig[i].size(); ++l) {
  54. start.push_back(heig[i][l]);
  55. }
  56. for (int j = 0; j < start.size(); ++j) {
  57. string s;
  58. int now = start[j];
  59. deque<int> d;
  60. for (int q = 0; q < i; ++q) {
  61. s = obr[now].second + s;
  62. d.push_front(now);
  63. now = obr[now].first;
  64. }
  65. d.push_front(now);
  66. auto it_1 = lower_bound(mer.begin(), mer.end(), s), it_2 = upper_bound(mer.begin(), mer.end(), s);
  67. it_2--;
  68. if (it_1 != mer.end() && it_1 == it_2) {
  69. for (int c = 0; c < d.size() - 1; ++c) {
  70. for (int h = c + 1; h < d.size(); ++h) {
  71. dp[d[c]][d[h]] = min(dp[d[c]][d[h]], (*it_1).second);
  72. }
  73. }
  74. }
  75. while (d[0] != 1) {
  76. s.pop_back();
  77. s = obr[now].second + s;
  78. d.pop_back();
  79. now = obr[now].first;
  80. d.push_front(now);
  81. auto it_1 = lower_bound(mer.begin(), mer.end(), s), it_2 = upper_bound(mer.begin(), mer.end(), s);
  82. it_2--;
  83. if (it_1 != mer.end() && it_1 == it_2) {
  84. for (int c = 0; c < d.size() - 1; ++c) {
  85. for (int h = c + 1; h < d.size(); ++h) {
  86. dp[d[c]][d[h]] = min(dp[d[c]][d[h]], (*it_1).second);
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. for (int i = 0; i <= n; ++i) {
  94. for (int l = 0; l <= n; ++l) {
  95. cout << dp[i][l] << " ";
  96. }
  97. cout << "\n";
  98. }
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement