Checkposts

Time Limit: 2000ms
Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 427C
64-bit integer IO format: %I64d      Java class name: (Any)

 
 
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).

 

Sample Input

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

Source

 
 
解题:强连通,看有多少个强联通块。每个强联通块里面就个最小的值,然后记录下这个块里,这样小的值有多少个!然后把所有块的最小值的数目乘起来,就是方案数,各块的最小值加起来,就是最小的那个什么什么。。。。注意要用长整型变量,注意要取模。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
const LL mod = ;
vector<int>g[maxn];
stack<int>s;
int w[maxn],dfn[maxn],low[maxn],n,m,id,scc;
bool instack[maxn];
LL cnt[maxn],sum,ways;
void tarjan(int u){
dfn[u] = low[u] = ++id;
instack[u] = true;
s.push(u);
int v;
for(v = ; v < g[u].size(); v++){
if(!dfn[g[u][v]]){
tarjan(g[u][v]);
low[u] = min(low[u],low[g[u][v]]);
}else if(instack[g[u][v]] && low[u] > dfn[g[u][v]])
low[u] = dfn[g[u][v]];
}
if(dfn[u] == low[u]){
int theMin = INF;
do{
v = s.top();
s.pop();
if(w[v] < theMin){
theMin = w[v];
cnt[scc] = ;
}else if(w[v] == theMin){
cnt[scc]++;
}
instack[v] = false;
}while(v != u);
scc++;
sum += theMin;
}
}
int main() {
int i,j,u,v;
while(~scanf("%d",&n)){
for(i = ; i <= n; i++)
scanf("%d",w+i);
for(i = ; i <= n; i++){
g[i].clear();
instack[i] = false;
low[i] = dfn[i] = ;
cnt[i] = ;
}
scanf("%d",&m);
for(i = ; i < m; i++){
scanf("%d %d",&u,&v);
g[u].push_back(v);
}
while(!s.empty()) s.pop();
sum = scc = id = ;
for(i = ; i <= n; i++)
if(!dfn[i]) tarjan(i);
for(ways = ,i = ; i < scc; i++){
ways = ((ways%mod)*(cnt[i]%mod))%mod;
}
printf("%I64d %I64d\n",sum,ways);
}
return ;
}

最新文章

  1. querySelectorAll 方法相比 getElementsBy 系列方法区别
  2. 记一次WinForm中屏蔽空格键对按钮的作用
  3. 通过sqlserver发送邮件
  4. Ubuntu 14 中,SecureCRT、SecureFX个性化设置
  5. UILable
  6. bootstrap学习总结-03 常用标签1
  7. iOS开发UI篇—UITableview控件简单介绍
  8. Ioc-Autofac实现自动的注入
  9. Unity -JsonUtility的使用
  10. mac cocos2dx android
  11. Alpha冲刺Day12
  12. oracle 表 库实例 空间
  13. PCL_common模块api代码解析
  14. 如何成为java高手
  15. Hadoop初期学习和集群搭建
  16. js如何将选中图片文件转换成Base64字符串?
  17. 黄聪:jquery.bootgrid表格插件有的属性(visibleInSelection、cssClass、headerCssClass、headerAlign)不能识别的解决办法
  18. Oracle 11g 在audit_file_dest目录下产生大量的aud文件
  19. js的等值比较规则
  20. WordPress For SAE进入后台

热门文章

  1. C plus plus sprintf用法
  2. Java标识符的习惯命名规范
  3. c#如何使用replace函数将&quot;\&quot;替换成&quot;\\&quot;
  4. 转 ORA-12638: 身份证明检索失败
  5. jmeter(十二)处理Cookie与Session
  6. 关于min-height:100%的解决办法
  7. Webform 内置对象 Response对象、Request对象,QueryString
  8. AJPFX关于java数组排序
  9. com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field &quot;FileSize&quot;
  10. React 实践心得:react-redux 之 connect 方法详解