File tree Expand file tree Collapse file tree 1 file changed +134
-0
lines changed
Expand file tree Collapse file tree 1 file changed +134
-0
lines changed Original file line number Diff line number Diff line change 1+ // JAI BAJARANG BALI
2+
3+ // manitianajay45
4+
5+ // give me some sunshine, give me some rain , give me another chance to grow up once again....
6+
7+ // sab moh maya hai....
8+
9+ /*
10+ steps:-
11+
12+ firstly merge all the strongly connected componenets and now graph is DAG .
13+ */
14+
15+ #include < bits/stdc++.h>
16+ using namespace std ;
17+
18+ #define ll long long
19+ #define mod 1000000007
20+ ll n,m;
21+
22+ vector<bool > visited (200005 ,false );
23+ vector<ll> graph[200005 ];
24+ vector<ll> order;
25+ vector<ll> graphr[200005 ];
26+ set<ll> gph[200005 ];
27+ vector<ll> vis (200005 ,0 );
28+ vector<ll> vec (200005 ,0 );
29+
30+ ll an=0 ;
31+ ll weight[200005 ];
32+ ll val=0 ;
33+
34+ vector<ll> ans (200005 ,-1 );
35+
36+
37+ void topo (ll v){
38+ visited[v]=true ;
39+ for (auto u:graph[v]){
40+ if (!visited[u]){
41+ topo (u);
42+ }
43+ }
44+ order.push_back (v);
45+ }
46+ bool flag=true ;
47+
48+ void dfs (ll v){
49+ // cout<<v<<" ";
50+ vis[v]=an;
51+ val+=weight[v];
52+ for (auto u:graphr[v]){
53+ if (vis[u]==0 ){
54+ dfs (u);
55+ }else if (vis[u]!=an){
56+ gph[vis[u]].insert (an);
57+ }
58+
59+ }
60+
61+ }
62+
63+
64+ ll mx=0 ;
65+
66+ ll ansfunc (ll v){
67+ if (ans[v]!=-1 ){
68+ return ans[v];
69+ }
70+ visited[v]=true ;
71+ ans[v]=vec[v];
72+ for (auto u:gph[v]){
73+ // if(!visited[u]){
74+ ans[v]=max (ans[v],vec[v]+ansfunc (u));
75+ // }
76+ }
77+ return ans[v];
78+ }
79+
80+
81+ int main ()
82+ {
83+ ios_base::sync_with_stdio (false );
84+ cin.tie (NULL );
85+
86+ // ll n;
87+ cin >> n>>m;
88+
89+ for (ll i=1 ;i<=n;i++){
90+ cin>>weight[i];
91+ }
92+
93+ vector<pair<ll,ll>> edges;
94+
95+ for (ll i = 0 ; i < m; i++)
96+ {
97+ ll u,v;
98+ cin>>u>>v;
99+ graph[u].push_back (v);
100+ graphr[v].push_back (u);
101+ edges.push_back ({u,v});
102+
103+ }
104+ for (ll i=1 ;i<=n;i++){
105+ if (!visited[i]){
106+ topo (i);
107+ }
108+ }
109+
110+ reverse (order.begin (),order.end ());
111+
112+ // vec.push_back(0);
113+ for (auto i:order){
114+ if (vis[i]==0 ){
115+ an++;
116+ val=0 ;
117+ dfs (i);
118+ vec[an]=val;
119+
120+ }
121+ }
122+
123+ for (ll i=1 ;i<=an;i++){
124+ if (ans[i]==-1 ){
125+ // cout<<i<<" ";
126+ mx=max (mx,ansfunc (i));
127+ // cout<<ansfunc(i)<<endl;````````````````````
128+ }
129+ }
130+ cout<<mx<<endl;
131+
132+
133+ return 0 ;
134+ }
You can’t perform that action at this time.
0 commit comments