forked from deutranium/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkosarajus.cpp
99 lines (97 loc) · 1.97 KB
/
kosarajus.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
// https : //codeforces.com/contest/427/problem/C
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod = 1e9 + 7;
stack<int> stk;
bool vis1[100005], vis2[100005];
int rep[100005];
vector<int> adj[100005], rev[100005];
vector<pair<int, int>> edges;
void trav(int n)
{
vis1[n] = true;
for (auto i : adj[n])
if (!vis1[i])
trav(i);
stk.push(n);
}
void trav2(int n, int id)
{
// cout<<"\t"<<id<<",";
vis2[n] = true;
for (auto i : rev[n])
if (!vis2[i])
trav2(i, id);
rep[n] = id;
// cout<<rep[n]<<" ";
}
void solve()
{
int n;
cin >> n;
int cost[n+1];
for (int i = 1; i <= n; i++)
cin >> cost[i];
int m;
cin >> m;
int x, y;
while (m--)
{
cin >> x >> y;
adj[x].push_back(y);
rev[y].push_back(x);
edges.push_back({x, y});
}
for (int i = 1; i <= n; i++)
if (!vis1[i])
trav(i);
int id = 1;
while (!stk.empty())
{
int t = stk.top();
stk.pop();
if (!vis2[t])
{
trav2(t, id);
id++;
}
}
// for (int i = 1; i <= n; i++)cout << rep[i] << " ";cout<<endl;
vector<int> minnie(id, INT_MAX), c(id+1, 0);
for (int i = 1; i <= n; i++)
{
if (minnie[rep[i]] == cost[i])
c[rep[i]]++;
else
{
if (cost[i] < minnie[rep[i]])
{
c[rep[i]] = 1;
minnie[rep[i]] = cost[i];
}
else
continue;
}
}
ll ans = 1, ans2 = 0;
// cout << endl;
for (int i = 1; i < id ; i++)
{
// cout << c[i] <<","<<minnie[i]<<" ";
ans = (ans * c[i]) % mod;
ans2 += minnie[i];
}
cout << ans2 << " " << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}