-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCoin Collector.cpp
123 lines (102 loc) · 2.63 KB
/
Coin Collector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
const int N = 200005;
vector<int> adj[N], adj_rev[N],adj_scc[N];
vector<bool> used;
vector<int> order, condensedOrder, component, root_nodes;
int sz[N];
ll res[N];
int roots[N];
ll coins[N];
void dfs1(int v) {
used[v] = true;
for (auto u : adj[v])
if (!used[u])
dfs1(u);
order.push_back(v);
}
void dfs2(int v) {
used[v] = true;
component.push_back(v);
for (auto u : adj_rev[v])
if (!used[u])
dfs2(u);
}
void solve(){
int n,m;
cin>>n>>m;
used.assign(n,false);
for(int i=0;i<n;i++) cin>>coins[i];
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
a--;
b--;
adj[a].push_back(b);
adj_rev[b].push_back(a);
}
for (int i = 0; i < n; i++)
if (!used[i])
dfs1(i);
used.assign(n, false);
reverse(order.begin(), order.end());
for (auto v : order){
if (!used[v]) {
dfs2 (v);
int root = component.front();
for (auto u : component) roots[u] = root;
root_nodes.push_back(root);
sz[root] = component.size();
component.clear();
}
}
for (int v = 0; v < n; v++){
for (auto u : adj[v]) {
int root_v = roots[v],
root_u = roots[u];
if (root_u != root_v)
adj_scc[root_v].push_back(root_u);
}
}
// topsort of the condensed graph
for(auto ord:order)
if( roots[ord] == ord ) condensedOrder.push_back(ord);
reverse(condensedOrder.begin(),condensedOrder.end());
// deleting multiple edges
for(auto root:root_nodes){
if( adj_scc[root].size()>=2 )
sort(adj_scc[root].begin(),adj_scc[root].end());
vector<int> nodes;
if( adj_scc[root].size() ) nodes.push_back( adj_scc[root][0] );
for(int i=1;i<adj_scc[root].size();i++){
if( adj_scc[root][i]!=adj_scc[root][i-1] ){
nodes.push_back(adj_scc[root][i]);
}
}
adj_scc[root] = nodes;
}
for(int i=0;i<n;i++) {
if( roots[i] == i ) continue;
coins[ roots[i] ]+=coins[i];
}
ll mx = 0;
for(auto ord:condensedOrder) {
for(int i=0;i<adj_scc[ord].size();i++){
res[ord]=max(res[ord],res[ adj_scc[ord][i] ]);
}
res[ord]+=coins[ord];
mx = max(mx, res[ord] );
}
cout<<mx<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc=1;
//cin>>tc;
for(int i=0;i<tc;i++){
solve();
}
}