Checkposts

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

3
1 2 3
3
1 2
2 3
3 2

Output

3 1

Input

5
2 8 0 6 0
6
1 4
1 3
2 4
3 4
4 5
5 1

Output

8 2

Input

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

15 6

Input

2
7 91
2
1 2
2 1

Output

7 1

题意:某个城市要在路口建哨岗,其中路口之间能构成环的建一个哨岗就行了(即有多少个环建多少个哨岗),在不同的路口建哨岗的花费不同,

问:1.每个环建一个哨岗,最少花费是?

        2.总共有多少种不同的建法(不管花费多少)对1e+7取模?

解题思路:用tarjan缩点,另开两个数组分别记录每个缩点内的最小花费,和缩点内的元素个数

#include<cstdio>
#include<stack>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=1e5+5;
stack<int>s;
vector<int>a[N];
int low[N],bfn[N],vis[N];
int k,tot,b[N],f[N];
int num[N],sum[N];
void dfs(int x)
{
	bfn[x]=low[x]=++tot;
	vis[x]=1;
	s.push(x);
	for(int i=0;i<a[x].size();i++)
	{
		int t=a[x][i];
		if(!bfn[t])
		{
			dfs(t);
			low[x]=min(low[x],low[t]);
		}
		else if(vis[t])low[x]=min(low[x],bfn[t]);
	}
	if(low[x]==bfn[x])
	{
		k++;
		sum[k]=f[s.top()];
		num[k]=0;
		while(!s.empty())
		{
			int t=s.top();
			s.pop();
			if(sum[k]>f[t])
			{
				sum[k]=f[t];
				num[k]=1;
			}
			else if(sum[k]==f[t])num[k]++;
			vis[t]=0;
		//	b[t]=k;
			if(x==t)break;
		}
	}
}
int main()
{
	int i,j;
	int n,m;
	ll x,y;
	cin>>n;
	for(i=1; i<=n; i++)cin>>f[i];
	cin>>m;
	for(i=0; i<m; i++)
	{
		cin>>x>>y;
		a[x].push_back(y);
	}
	for(i=1; i<=n; i++)
		if(!bfn[i])
			dfs(i);
	x=0;
	y=1;
	for(i=1; i<=k; i++)
	{
		x+=sum[i];
		y=(y*num[i])%mod;
	}
	printf("%lld %lld\n",x,y);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_52898168/article/details/119561788