Codeforces#427C Checkposts 【Tarjan求scc 】+【同余定理】

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).

Example
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<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<iostream>
#include<vector>
#define LL long long
using namespace std;
const int MAXN= 1e5+100;
const int MAXM= 1e8;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch>'9'||ch<'0'){ if(ch=='-') f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
/*------------------------------------*/
struct Edge {
    int from,to,next;
}edge[MAXN<<2];
int head[MAXN],top;
int n,m;
int cost[MAXN];
void init(){
    memset(head,-1,sizeof(head));
    top=0;
}
void addedge(int a,int b){
    Edge e={a,b,head[a]};
    edge[top]=e;head[a]=top++;
}
void getmap(){
    for(int i=1;i<=n;i++) cost[i]=read();

    int a,b;
    cin>>m;while(m--){
        a=read();b=read();
        addedge(a,b);
    }
}
int dfn[MAXN],low[MAXN];
int dfs_clock;
int sccno[MAXN],scc_cnt;
bool Instack[MAXN];stack<int>S;
vector<int>scc[MAXN];
void tarjan(int now){
    dfn[now]=low[now]=++dfs_clock;
    S.push(now);Instack[now]=true;
    for(int i=head[now];i!=-1;i=edge[i].next){
        Edge e=edge[i];
        if(!dfn[e.to]){
            tarjan(e.to);
            low[now]=min(low[now],low[e.to]);
        }else if(Instack[e.to])
        low[now]=min(low[now],dfn[e.to]);
    }
    if(low[now]==dfn[now]){
        scc_cnt++;scc[scc_cnt].clear();
        for(;;){
            int nexts=S.top();S.pop();Instack[nexts]=false;
            scc[scc_cnt].push_back(nexts);
            sccno[nexts]=scc_cnt;
            if(now==nexts) break;
        }
    }
}

void find_cut(int le,int ri){
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(Instack,0,sizeof(Instack));
    memset(sccno,0,sizeof(sccno));
    dfs_clock=scc_cnt=0;
    for(int i=le;i<=ri;i++){
        if(!dfn[i]) tarjan(i);
    }
}
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
void solve(){
    find_cut(1,n);
    LL mincost=0;LL ways=1;//  这里wa了一发,要用LL 

    /*for(int i=1;i<=scc_cnt;i++){
        for(int j=0;j<scc[i].size();j++){
            printf("%d ",scc[i][j]);
        }
        putchar('\n');
    }*/

    for(int i=1;i<=scc_cnt;i++){
        LL cnt=0;int sum=inf;
        for(int j=0;j<scc[i].size();j++){
             sum=min(sum,cost[scc[i][j]]);
        }
        mincost=mincost+(LL)sum;
        for(int j=0;j<scc[i].size();j++){
              if(sum==cost[scc[i][j]])
                cnt++;
        }
        ways=(ways%mod*cnt%mod)%mod;
    }
    printf("%lld %lld\n",mincost,ways);
}
int main(){
        n=read();
        init();
        getmap();
        solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值