-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkosaraju-algorithm.cpp
More file actions
84 lines (80 loc) · 1.68 KB
/
Copy pathkosaraju-algorithm.cpp
File metadata and controls
84 lines (80 loc) · 1.68 KB
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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod (int)(1e9+7)
#define sz(s) (int)s.size()
#define all(v) v.begin(),v.end()
#define input(v) for (auto &i : v) cin >> i;
#define print(v) for (auto &j : v) cout << j << " "; cout << "\n";
vector<int>comp,order;
vector<bool>visited;
vector<vector<int>>adj,rev_adj;
void dfs1(int s)
{
if(visited[s]) return;
visited[s]=true;
for(auto u:adj[s]) dfs1(u);
order.push_back(s);
}
void dfs2(int s)
{
if(visited[s]) return;
visited[s]=true;
for(auto u:rev_adj[s]) dfs2(u);
comp.push_back(s);
}
void solve()
{
int n,cst=0,ways=1;
cin>>n;
vector<int>cost(n);
input(cost);
int m;
cin>>m;
adj.resize(n+1);
rev_adj.resize(n+1);
while(m--)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
rev_adj[v].push_back(u);
}
visited.assign(n+1,false);
for(int i=1;i<=n;i++)
{
if(!visited[i]) dfs1(i);
}
reverse(all(order));
for(int i=0;i<=n;i++) visited[i]=false;
for(auto u:order)
{
if(!visited[u])
{
comp.clear();
dfs2(u);
int mncost=1e18;
int cnt=0;
for(auto x:comp)
{
mncost=min(mncost,cost[x-1]);
}
for(auto x:comp)
{
if(mncost == cost[x-1]) cnt++;
}
cst+=mncost;
ways = ways*cnt%mod;
}
}
cout<<cst<<" "<<ways<<"\n";
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int testcases=1;
//cin>>testcases;
while(testcases--) solve();
return 0;
}