Advertisement
BaoJIaoPisu

Untitled

Feb 24th, 2022
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using ld = long double;
  7. using ull = unsigned long long;
  8.  
  9. using pii = pair<int, int>;
  10. using pll = pair<ll, ll>;
  11. using pld = pair<ld, ld>;
  12.  
  13. #define fi first
  14. #define se second
  15. #define pb push_back
  16. #define pf push_front
  17. #define mp make_pair
  18. #define ins insert
  19. #define btpc __builtin_popcount
  20. #define btclz __builtin_clz
  21.  
  22. #define sz(x) (int)(x.size());
  23. #define all(x) x.begin(), x.end()
  24. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  25.  
  26. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  27.  
  28. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  29. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  30. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  31.  
  32. template<class X, class Y>
  33. bool minimize(X &x, const Y &y) {
  34. if (x > y)
  35. {
  36. x = y;
  37. return true;
  38. }
  39. return false;
  40. }
  41. template<class X, class Y>
  42. bool maximize(X &x, const Y &y) {
  43. if (x < y)
  44. {
  45. x = y;
  46. return true;
  47. }
  48. return false;
  49. }
  50.  
  51. const int MOD = 1e9 + 7; //998244353
  52.  
  53. template<class X, class Y>
  54. void add(X &x, const Y &y) {
  55. x = (x + y);
  56. if(x >= MOD) x -= MOD;
  57. }
  58.  
  59. template<class X, class Y>
  60. void sub(X &x, const Y &y) {
  61. x = (x - y);
  62. if(x < 0) x += MOD;
  63. }
  64.  
  65. /* Author : Le Ngoc Bao Anh, 11A5, LQD High School for Gifted Student*/
  66.  
  67. const ll INF = 1e9;
  68. const int N = 3e5 + 10;
  69.  
  70. struct TrieNode {
  71. TrieNode* child[26];
  72. ll dp = 0;
  73.  
  74. TrieNode() {
  75. for(int i = 0; i < 26; i++) child[i] = nullptr;
  76. dp = 0;
  77. }
  78. };
  79.  
  80. string s[N];
  81. int p[N];
  82. vector<int> g[N], imp[N], nimp[N];
  83. bool is_imp[N];
  84. ll dp[N], f[N], b[N];
  85.  
  86. void solve() {
  87. int n, m; cin >> n >> m;
  88. for(int i = 1; i <= n; i++) {
  89. cin >> s[i] >> p[i];
  90. }
  91.  
  92. int S = 600;
  93. TrieNode* root = new TrieNode();
  94.  
  95. auto updateTrie = [&](string s, ll x) -> void {
  96. int n = s.size();
  97. TrieNode* curr = root;
  98.  
  99. for(int i = 0; i < n; i++) {
  100. int p = (s[i] - 'A');
  101. if(curr->child[p] == nullptr) curr->child[p] = new TrieNode();
  102. curr = curr->child[p];
  103. if(i + 1 < n) maximize(curr->dp, x);
  104. }
  105. };
  106.  
  107. auto getTrie = [&](string s) -> ll {
  108. int n = s.size();
  109. TrieNode* curr = root;
  110.  
  111. for(int i = 0; i < n; i++) {
  112. int p = (s[i] - 'A');
  113. if(curr->child[p] == nullptr) curr->child[p] = new TrieNode();
  114. curr = curr->child[p];
  115. }
  116.  
  117. return curr->dp;
  118. };
  119.  
  120. for(int i = 1; i <= m; i++) {
  121. int u, v; cin >> u >> v;
  122. g[u].pb(v);
  123. g[v].pb(u);
  124. }
  125.  
  126. int t = 1e5;
  127. for(int i = 1; i <= t; i++) {
  128. sort(all(g[i]));
  129. g[i].resize(unique(all(g[i])) - g[i].begin());
  130. }
  131.  
  132. for(int i = 1; i <= t; i++) {
  133. is_imp[i] = (g[i].size() >= S);
  134. }
  135.  
  136. for(int i = 1; i <= t; i++) {
  137. for(auto u : g[i]) {
  138. if(is_imp[u]) imp[i].pb(u);
  139. else nimp[i].pb(u);
  140. }
  141. }
  142.  
  143. ll ans = 0;
  144. for(int i = 1; i <= n; i++) {
  145. dp[i] = getTrie(s[i]) + p[i];
  146. int u = p[i];
  147. maximize(dp[i], f[u] + u);
  148. for(auto v : imp[u]) maximize(dp[i], b[v] + p[i]);
  149.  
  150. if(is_imp[u]) maximize(b[u], dp[i]);
  151. else {
  152. for(auto v : g[u]) maximize(f[v], dp[i]);
  153. }
  154.  
  155. updateTrie(s[i], dp[i]);
  156. maximize(ans, dp[i]);
  157. }
  158.  
  159. cout << ans;
  160. }
  161.  
  162. int main()
  163. {
  164. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  165. #ifndef ONLINE_JUDGE
  166. freopen("input.txt", "r", stdin);
  167. freopen("output.txt", "w", stdout);
  168. #else
  169. //online
  170. #endif
  171.  
  172. int tc = 1, ddd = 0;
  173. // cin >> tc;
  174. while(tc--) {
  175. //ddd++;
  176. //cout << "Case #" << ddd << ": ";
  177. solve();
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement