fork download
  1. /**
  2. - author: orzvanh14 ( )
  3. - created: 23.12.2022 10:08:02
  4. - too lazy to update time
  5. **/
  6. // i wants to take ioi
  7. //binhtinhtutinkhongcaycunhungmotkhikhongcontutinnualatuyetvong
  8. #include <bits/stdc++.h>
  9.  
  10. using namespace std;
  11.  
  12. #define int long long
  13. #define nn "\n"
  14. #define pi pair<int, int>
  15. #define fi first
  16. #define se second
  17. #define lb lower_bound
  18. #define ub upper_bound
  19. #define eb emplace_back
  20. #define pb push_back
  21. #define TASK " "
  22.  
  23. #define ms(a, x) memset(a, x, sizeof(a))
  24. #define all(a) a.begin(), a.end()
  25. #define All(a, n) a + 1, a + 1 + n
  26.  
  27. #define LOG 21
  28.  
  29. typedef __int128 i128;
  30.  
  31. const int INF = 1e18;
  32. const int mod = 1e9 + 7;
  33. const int maxN = 1e6 + 5;
  34. const int N = 1e5 + 5;
  35. int MOD = 998244353;
  36. int bit[200000];
  37.  
  38. struct node{
  39. int kc, u, v;
  40. bool operator<(const node& other) const {
  41. return kc > other.kc;
  42. }
  43. };
  44. struct edge{
  45. int u, v, w;
  46. bool operator<(const edge& other) const {
  47. return w < other.w;
  48. }
  49. };
  50. struct Node{
  51. int r, c, kc, id;
  52. bool operator<(const Node& other) const {
  53. return kc > other.kc;
  54. }
  55. };
  56.  
  57. int n, m;
  58. int coin[N];
  59. vector<int> adj[N];
  60. int disc[N], low[N], scc_id[N];
  61. bool in_stack[N];
  62. stack<int> st;
  63. int timer = 0, scc_count = 0;
  64. void tarjan(int u) {
  65. disc[u] = low[u] = ++timer;
  66. st.push(u);
  67. in_stack[u] = true;
  68. for (int v : adj[u]) {
  69. if (disc[v] == -1) {
  70. tarjan(v);
  71. low[u] = min(low[u], low[v]);
  72. } else if (in_stack[v]) {
  73. low[u] = min(low[u], disc[v]);
  74. }
  75. }
  76. if (low[u] == disc[u]) {
  77. while (true) {
  78. int v = st.top();
  79. st.pop();
  80. in_stack[v] = false;
  81. scc_id[v] = scc_count;
  82. if (u == v) break;
  83. }
  84. scc_count++;
  85. }
  86. }
  87.  
  88. void nhap(){
  89. cin >> n >> m;
  90. for(int i = 1; i <= n; i++){
  91. cin >> coin[i];
  92. }
  93. for(int i = 1; i <= m; i++){
  94. int u, v;
  95. cin >> u >> v;
  96. adj[u].eb(v);
  97. }
  98. }
  99.  
  100. void solve(){
  101. for(int i = 1; i <= n; i++){
  102. disc[i] = -1;
  103. low[i] = -1;
  104. scc_id[i] = -1;
  105. in_stack[i] = false;
  106. }
  107. for(int i = 1; i <= n; i++){
  108. if(disc[i] == -1){
  109. tarjan(i);
  110. }
  111. }
  112. vector<int> scc_coin(scc_count, 0);
  113. for(int i = 1; i <= n; i++){
  114. scc_coin[scc_id[i]] += coin[i];
  115. }
  116. vector<vector<int>> dag(scc_count);
  117. vector<int> in_deg(scc_count, 0);
  118. for(int u = 1; u <= n; u++){
  119. for(int v : adj[u]){
  120. int cu = scc_id[u], cv = scc_id[v];
  121. if(cu != cv){
  122. dag[cu].eb(cv);
  123. in_deg[cv]++;
  124. }
  125. }
  126. }
  127. queue<int> q;
  128. vector<int> dp(scc_count, 0);
  129. for(int c = 0; c < scc_count; c++){
  130. dp[c] = scc_coin[c];
  131. if(in_deg[c] == 0){
  132. q.push(c);
  133. }
  134. }
  135. int ans = 0;
  136. while(!q.empty()){
  137. int cu = q.front();
  138. q.pop();
  139. ans = max(ans, dp[cu]);
  140. for(int cv : dag[cu]){
  141. dp[cv] = max(dp[cv], dp[cu] + scc_coin[cv]);
  142. in_deg[cv]--;
  143. if(in_deg[cv] == 0){
  144. q.push(cv);
  145. }
  146. }
  147. }
  148. cout << ans << nn;
  149. }
  150.  
  151. signed main() {
  152. // freopen("Bai4.inp", "r", stdin);
  153. // freopen("Bai4.out", "w", stdout);
  154. ios_base::sync_with_stdio(0);
  155. cin.tie(0);
  156. cout.tie(0);
  157. nhap();
  158. solve();
  159. return (0 ^ 0);
  160. }
Success #stdin #stdout 0s 6544KB
stdin
4 4
4 5 2 7
1 2
2 1
1 3
2 4
stdout
16