-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1686 Coin Collector.cpp
134 lines (103 loc) · 2.14 KB
/
1686 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
123
124
125
126
127
128
129
130
131
132
133
134
// JAI BAJARANG BALI
// manitianajay45
// give me some sunshine, give me some rain , give me another chance to grow up once again....
// sab moh maya hai....
/*
steps:-
firstly merge all the strongly connected componenets and now graph is DAG .
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
ll n,m;
vector<bool> visited(200005,false);
vector<ll> graph[200005];
vector<ll> order;
vector<ll> graphr[200005];
set<ll> gph[200005];
vector<ll> vis(200005,0);
vector<ll> vec(200005,0);
ll an=0;
ll weight[200005];
ll val=0;
vector<ll> ans(200005,-1);
void topo(ll v){
visited[v]=true;
for(auto u:graph[v]){
if(!visited[u]){
topo(u);
}
}
order.push_back(v);
}
bool flag=true;
void dfs(ll v){
// cout<<v<<" ";
vis[v]=an;
val+=weight[v];
for(auto u:graphr[v]){
if(vis[u]==0){
dfs(u);
}else if(vis[u]!=an){
gph[vis[u]].insert(an);
}
}
}
ll mx=0;
ll ansfunc(ll v){
if(ans[v]!=-1){
return ans[v];
}
visited[v]=true;
ans[v]=vec[v];
for(auto u:gph[v]){
// if(!visited[u]){
ans[v]=max(ans[v],vec[v]+ansfunc(u));
// }
}
return ans[v];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// ll n;
cin >> n>>m;
for(ll i=1;i<=n;i++){
cin>>weight[i];
}
vector<pair<ll,ll>> edges;
for (ll i = 0; i < m; i++)
{
ll u,v;
cin>>u>>v;
graph[u].push_back(v);
graphr[v].push_back(u);
edges.push_back({u,v});
}
for(ll i=1;i<=n;i++){
if(!visited[i]){
topo(i);
}
}
reverse(order.begin(),order.end());
// vec.push_back(0);
for(auto i:order){
if(vis[i]==0){
an++;
val=0;
dfs(i);
vec[an]=val;
}
}
for(ll i=1;i<=an;i++){
if(ans[i]==-1){
// cout<<i<<" ";
mx=max(mx,ansfunc(i));
// cout<<ansfunc(i)<<endl;````````````````````
}
}
cout<<mx<<endl;
return 0;
}