codeforces 472C Checkposts 强连通

被long long WA了三次。。


Checkposts
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Your city has n junctions. There are mone-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 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





#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>

using namespace std;

#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8

typedef long long LL;

const int maxn=3*1e5+5;

const int mod=1000000007;


struct node{
    int u,to,next;
}e[maxn];

int head[maxn],edge;

struct nodeA{
    int nums,j;
}id[maxn];

int q[maxn],dfn[maxn],low[maxn],num[maxn];


int n,m,tsp,qe,cnt;

void init()
{
    MST(head,-1);
    edge=0;
}

void addedge(int u,int v)
{
    e[edge].u=u;
    e[edge].to=v;
    e[edge].next=head[u];
    head[u]=edge++;
}

void tarjan(int u)
{
    int i,v;
    dfn[u]=low[q[qe++]=u]=++tsp;
    for(i=head[u];i>=0;i=e[i].next)
        if(!dfn[v=e[i].to])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else
        {
            if(id[v].nums<0)
            {
                low[u]=min(low[u],dfn[v]);
            }
        }
    if(low[u]==dfn[u])
    {
        num[id[u].nums=++cnt]=1;
        id[u].j=u;
        while((v=q[--qe])!=u)
            {
                ++num[id[v].nums=cnt];
                id[v].j=v;
            }
    }
}

int cmp(nodeA a,nodeA b)
{
    return a.nums<b.nums;
}


void solve()
{
    int i;
    tsp=qe=cnt=0;
    for(i=0;i<=n;++i)
        id[i].nums=-1,dfn[i]=0;
    for(i=1;i<=n;++i)
        if(!dfn[i])
            tarjan(i);
}


int a[111115];

int main()
{
    init();
    cin>>n;
    CLR(a);
    FOR(i,1,n)
     scanf("%d",&a[i]);
    cin>>m;
    int u,v;
    REP(i,m)
    {
        scanf("%d%d",&u,&v);
        addedge(u,v);
    }
    solve();
    sort(id+1,id+1+n,cmp);
    LL ans=0;
    int pos=1;
    int res=0;
    LL sum=1;
    //cout<<cnt<<endl;
    int flag=0;
    FOR(i,1,cnt)
    {
        int minn=INF;
        FOR(k,pos,n+1)
        {
            if(id[k].nums == i)
                minn=min(minn,a[id[k].j]);
            else
            {
                //cout<<pos<<" "<<k-1<<endl;
                res=0;
                FOR(h,pos,k-1)
                 if(a[id[h].j] == minn)
                    res++;

                pos=k;
                break;
            }
           // if(k==n)
                //flag=1;

        }
       /* if(flag)
        {
            res=0;
            FOR(h,pos,n)
             if(a[id[h].j]==minn)
                    res++;
        }*/
        //cout<<res<<endl;
        ans+=minn;
        sum=sum*res;
        //cout<<res<<endl;
    }
    cout<<ans<<" "<<sum%mod<<endl;
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值