Algorithm


C. Checkposts
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.

To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either i = j or the police patrol car can go to j from i and then come back to i.

Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.

You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.

Input

In the first line, you will be given an integer n, number of junctions (1 ≤ n ≤ 105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109).

The next line will contain an integer m (0 ≤ m ≤ 3·105). And each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ nu ≠ v). A pair ui, vi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.

Output

Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109 + 7).

Examples
input
Copy
3
1 2 3
3
1 2
2 3
3 2
output
Copy
3 1
input
Copy
5
2 8 0 6 0
6
1 4
1 3
2 4
3 4
4 5
5 1
output
Copy
8 2
input
Copy
10
1 3 2 2 1 3 1 4 10 10
12
1 2
2 3
3 1
3 4
4 5
5 6
5 7
6 4
7 3
8 9
9 10
10 9
output
Copy
15 6
input
Copy
2
7 91
2
1 2
2 1
output
Copy
7 1

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int const N = 1e5 + 1, MOD = 1e9 + 7;
int n, m, a[N], dfs, comp, idx[N], low[N];
bool in[N];
vector<int> st;
vector<vector<int> > g, comps;

void DFS(int v) {
  low[v] = idx[v] = dfs++;
  in[v] = true;
  st.push_back(v);

  for(int i = 0; i < g[v].size(); i++)
    if(idx[g[v][i]] == -1) {
      DFS(g[v][i]);
      low[v] = min(low[v], low[g[v][i]]);
    } else if(in[g[v][i]])
      low[v] = min(low[v], low[g[v][i]]);

  if(low[v] == idx[v]) {
    int node;
    do {
      node = st.back();
      in[node] = false;
      st.pop_back();
      comps[comp].push_back(node);
    } while(node != v);

    comp++;
  }
}

int main() {
	scanf("%d", &n);
	g.resize(n);
	comps.resize(n);
	for(int i = 0; i < n; ++i)
		scanf("%d", a + i);
	scanf("%d", &m);
	for(int i = 0, a, b; i < m; ++i) {
		scanf("%d %d", &a, &b);
		--a, --b;
		g[a].push_back(b);
	}

	memset(idx, -1, sizeof idx);
	memset(in, false, sizeof in);
	dfs = comp = 0;
  for(int i = 0; i < n; i++)
    if(idx[i] == -1)
			DFS(i);

	long long res = 0, ways = 1;
	for(int i = 0; i < comps.size(); ++i, puts("")) {
		if(comps[i].empty())
			break;

		int mnv = 2e9, mni;
		for(int j = 0; j < comps[i].size(); ++j)
			if(a[comps[i][j]] < mnv)
				mnv = a[comps[i][j]], mni = comps[i][j];
		res += a[mni];

		int rep = 0;
		for(int j = 0; j < comps[i].size(); ++j)
			if(a[comps[i][j]] == a[mni])
				++rep;
		ways = ways * rep % MOD;
	}

	printf("%lld %lld\n", res, ways);

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
1 2 3
3
1 2
2 3
3 2

Output

x
+
cmd
3 1
Advertisements

Demonstration


Codeforces Solution-Checkposts-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+