http://www.elijahqi.win/archives/939
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 ican 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 andvi (1 ≤ ui, vi ≤ n; u ≠ 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
求出每个强联通的最小值得个数和最小值是什么
#include<cstdio>
#include<stack>
#define N 110000
#define mod 1000000007
#define inf 0x7f7f7f7f
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct node{
int y,z,next;
}data[330000];
int dfn[N],low[N],num,size[N],a[N],h[N],s,b[N],n,m,min1[N];bool stackf[N];
inline int min(int x,int y){
return x<y?x:y;
}
std::stack<int> q;
void tarjan(int x){
dfn[x]=++num;low[x]=num;q.push(x);stackf[x]=true;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (!dfn[y]) tarjan(y),low[x]=min(low[x],low[y]);else if (stackf[y]) low[x]=min(low[x],dfn[y]);
}
if (dfn[x]==low[x]){
int y;s++;min1[s]=inf;
do{
y=q.top();b[y]=s;q.pop();stackf[y]=false;
if (a[y]<min1[s]) min1[s]=a[y],size[s]=1;else if (a[y]==min1[s]) size[s]++;
}while (y!=x);
}
}
int main(){
freopen("cf.in","r",stdin);
n=read();
for (int i=1;i<=n;++i) a[i]=read();m=read();
for (int i=1;i<=m;++i){
int x=read(),y=read();
data[i].y=y;data[i].next=h[x];h[x]=i;
}
for (int i=1;i<=n;++i) if (!dfn[i]) tarjan(i);
long long ans1=1,ans=0;
for (int i=1;i<=s;++i) ans+=min1[i],(ans1*=size[i])%=mod;
printf("%lld %lld",ans,ans1);
return 0;
}