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 ;
}

xtu summer individual 6 D - Checkposts的更多相关文章

  1. xtu summer individual 4 C - Dancing Lessons

    Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  2. xtu summer individual 3 C.Infinite Maze

    B. Infinite Maze time limit per test  2 seconds memory limit per test  256 megabytes input standard ...

  3. xtu summer individual 2 E - Double Profiles

    Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  4. xtu summer individual 2 C - Hometask

    Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...

  5. xtu summer individual 1 A - An interesting mobile game

    An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on H ...

  6. xtu summer individual 2 D - Colliders

    Colliders Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...

  7. xtu summer individual 1 C - Design the city

    C - Design the city Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu D ...

  8. xtu summer individual 1 E - Palindromic Numbers

    E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %l ...

  9. xtu summer individual 1 D - Round Numbers

    D - Round Numbers Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u D ...

随机推荐

  1. _bzoj1051 [HAOI2006]受欢迎的牛【强联通】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1051 保存tarjan模版. 求强联通分量,缩点. #include <cstdio& ...

  2. vijos1846 [NOIP2013] 华容道【最短路】

    传送门:https://vijos.org/p/1983 (其实noip的题各个oj都会有的,就不贴其它传送门了) 这道题真的是,怎么说,我都不知道怎么评价了= =.果然数据量小的题怎么暴力都可以过. ...

  3. 414 Third Maximum Number 第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n).示例 1:输入: [3, 2, 1]输出: 1解释: 第三大的数是 1.示例 2:输入: ...

  4. 转】用Nodejs连接MySQL

    原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/2/ 感谢! 用Nodejs连接MySQL 从零开始node ...

  5. Activiti工作流和shiro权限管理关系图

  6. 5.1点击4个按钮显示相应的div

    事件:onclick 属性:display,className 用到for语句,index标记,this当前事件 先清空后附加 <!DOCTYPE html><html>< ...

  7. 数据库text字段存值用回车分隔

    //查询 $sql = "SELECT attr_values FROM ecs_attribute WHERE attr_id=197"; $param_sel_sms = ar ...

  8. Java MVC框架性能比较

    Java MVC框架性能比较 - by zvane 现在各种MVC框架很多,各框架的优缺点网络上也有很多的参考文章,但介绍各框架性能方面差别的文章却不多,本人在项目开发中,感觉到采用了struts2框 ...

  9. Elasticsearch の 查询类型

    查询类型SearchType Es中一共有四种查询类型:QUERY_AND_FETCH.QUERY_THEN_FETCH.DFS_QUERY_AND_FETCH.DFS_QUERY_THEN_FETC ...

  10. Android(java)学习笔记190:ContentProvider使用之学习ContentProvider(内容提供者)的目的

    1. 使用ContentProvider,把应用程序私有的数据暴露给别的应用程序,让别的应用程序完成对自己私有的数据库数据的增删改查的操作. 2. ContentProvider的应用场景: 获取手机 ...