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. QT如何发布程序

    QT如何发布程序转载 http://blog.csdn.net/iw1210/article/details/51253458

  2. Data Center Maintenance CodeForces - 950E

    http://codeforces.com/contest/950/problem/E 贴一份板子 #include<cstdio> #include<vector> #inc ...

  3. Python基础第一天

    诞生时间:1991年,创造者Guido van Rossum 优点: 1.简单  Python是一种代表简单注意思想的语言 2.易学  Python是及其容易上手,因为Python有极其简单的说明文档 ...

  4. AJPFX关于构造器的总结

    构造器        构造器定义        构造器作用        构造器特点        构造器修饰符        默认构造器        构造器重载        构造器和一般函数的区 ...

  5. AJPFX简述java语言现状和发展

    作为一种最流行的网络编程语言之一,java语言在当今信息化社会中发挥了 重要的作用.Java语言具有面向对象.跨平台.安全性.多线程等特点,这使得java成为许多应用系统的理想开发语言.java应用在 ...

  6. AJPFX关于多态中的动态绑定和静态绑定的总结

    在多态中:成员变量和静态方法编译和运行都看左边:成员方法编译看左边,运行看右边,这是为什么:在Java中存在两种绑定方式,一种为静态绑定,又称作早期绑定.另一种就是动态绑定,亦称为后期绑定1.静态绑定 ...

  7. ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第二天(非原创)

    文章大纲 一.课程介绍二.整合淘淘商城ssm项目三.Mybatis分页插件PageHelper使用四.整合测试五.项目源码与资料下载六.参考文章   一.课程介绍 一共14天课程(1)第一天:电商行业 ...

  8. JavaScript异常处理和事件处理

    异常捕获 1.异常:      当JavaScript引擎执行JavaScript代码时,发生了错误,导致程序停止运行 2.异常抛出:      当异常产生,并且将这个异常生成一个错误信息 3.异常捕 ...

  9. css中border制作各种形状

    css利用border制作各种形状的原理如图: 使用border绘制三角形是什么原理?事实上,宽度相等的border是以45度对接的,如下图: 没有了上border如图所示: 再设置border的宽度 ...

  10. 移动设备访问使用百度js跳转

    以下为代码,可放置在网站foot底部文件,或者haead顶部文件,建议将代码放在网站顶部,这样可以实现手机访问立即跳转! <script src="http://siteapp.bai ...