Codeforces 427 C. Checkposts

来源:互联网 发布:惠惠购物助手mac版 编辑:程序博客网 时间:2024/05/16 15:52

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 ≤ 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

独立思考强联通第一题2333敲开心的
这题吧2333强联通 先tarjan然后缩点 对每个缩成点的连通块找出里面费用的最小值。把所有连通块的费用最小值加起来就是总费用。
第二个让你求这样的最小值有几种方案。2333我这种智障数学也知道是乘法公式。求每个连通块里的最小值有几个,然后把所有连通块里最小值的数量乘起来就是第二个答案。

要用long long存答案 WA了一次QWQ

#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<vector>#include<cmath>#include<cstdio>#include<cstring>#include<stack>#include<string>using namespace std;//thanks to pyf ...#define INF 0x3f3f3f3f#define CLR(x,y) memset(x,y,sizeof(x))const int  MOD  = 1e9+7 ;typedef pair<int,int> PII;const int N = 1e5+5;struct Edge{    int u,v,next;}edge[N*3];int size[N];int cost[N];int ans[N];int head[N];int dfn[N];int low[N];int belong[N];int instack[N];stack<int>st;int tot = 0;int cnt = 0;int Index = 0;int n;void init(){    tot = 0;    Index = 0;    cnt = 0;    CLR(head,-1);    CLR(dfn,0);    CLR(low,0);    CLR(belong,0);    CLR(instack,0);    CLR(ans,INF);    CLR(cost,0);    CLR(size,0);    while(!st.empty())        st.pop();}void add_edge(int u,int v){    edge[tot].u = u;    edge[tot].v = v;    edge[tot].next = head[u];    head[u] = tot++;}void tarjan(int u){    dfn[u] = low[u] = ++Index;    st.push(u);    instack[u] = 1;    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v = edge[i].v;        if(!dfn[v])        {            tarjan(v);            low[u] = min(low[u],low[v]);        }        else if(instack[v])            low[u] = min(low[u],dfn[v]);    }    int x;    if(dfn[u]==low[u])    {        cnt ++;        do{            x = st.top();            st.pop();            belong[x] = cnt;            instack[x] = 0;        }while(x!=u);    }}void solve(){    for(int i=1;i<=n;i++)    {        if(!dfn[i])            tarjan(i);    }}int main(){    while(scanf("%d",&n)==1)    {        init();        for(int i=1;i<=n;i++)            scanf("%d",cost+i);        int m;        scanf("%d",&m);        for(int i=0;i<m;i++)        {            int u,v;            scanf("%d%d",&u,&v);            add_edge(u,v);        }        solve();        for(int i=1;i<=n;i++)        {            ans[belong[i]] = min(ans[belong[i]],cost[i]);        }        for(int i=1;i<=n;i++)        {            if(ans[belong[i]]==cost[i])                size[belong[i]]++;        }        long long total = 0;        long long solution = 1;        for(int i=1;i<=cnt;i++)        {            total+=ans[i];            solution = ((solution%MOD)*(size[i]%MOD))%MOD;        }        printf("%lld %lld\n",total,solution);    }}
0 0
原创粉丝点击