Codeforces 427C Checkposts【Tarjan强连通+缩点染色】

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

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

题目大意:

一共给你N个点,M条有向边。其中每个点都有其自己对应的权值,作为城市的市长,你希望设定警察局来保护所有的城市。

如果我们在点i处设立了一个警察局,那么其点i是被保护的,而且如果一个点j,能够保证有路径从i到j,并且能够保证有路径从j回到i,那么点j也是被保护的。

问将所有城市都保护起来的最小花费,以及对应最小花费有多少种设定的方式。


思路:


1、题干中透露的信息已经足足够多了,如果一个点从i到j,并且从j能够到达i,那么两点是属于同一个强连通分量的。我们为了得到最小花费,那么我们首先考虑统计强连通分量的个数,以及对每个点进行染色的工作。所以我们这里需要用到强连通分量相关算法,(我这里使用的是Tarjan,其他类似Kosaraju这个题给出的点的个数以及时间限制也都是可以hold住的。)对这N个点进行缩点染色的工作。


2、将所有点都缩点染色之后,我们考虑每个颜色的点的最小花费点:

①考虑到一共有1e5个点,如果每个点都是孤立的,那么我们O(n^2)暴力维护每个颜色的最小花费是会TLE的。

②对应我们设定两个数组:minn【i】表示第i个颜色的最小花费点的权值,contz【i】表示第i个颜色的最小花费点的权值的点的个数。


那么我们累加minn【i】,累乘contz【i】,就是两个需要输出的答案。


注意别忘了取模即可。


Ac代码:


#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define mod  1000000007
#define ll __int64
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[1515151];
int vis[100400];
int stack[100400];
int color[100400];
int head[100400];
int dfn[100400];
int low[100400];
int a[100400];
int minn[100400];
int contz[100400];
int n,cont,sig,cnt,tot;
void add(int from,int to)
{
    e[cont].from=from;
    e[cont].to=to;
    e[cont].next=head[from];
    head[from]=cont++;
}
void init()
{
    cont=0;
    sig=0;
    cnt=1;
    tot=-1;
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(stack,0,sizeof(stack));
    memset(color,0,sizeof(color));
    memset(vis,0,sizeof(vis));
}
void Tarjan(int u)
{
    stack[++tot]=u;
    vis[u]=1;
    low[u]=dfn[u]=cnt++;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(vis[v]==0)Tarjan(v);
        if(vis[v]==1)low[u]=min(low[u],low[v]);
    }
    if(low[u]==dfn[u])
    {
        sig++;
        do
        {
            color[stack[tot]]=sig;
            vis[stack[tot]]=-1;
        }
        while(stack[tot--]!=u);
    }
}
void Slove()
{
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==0)
        {
            Tarjan(i);
        }
    }
    for(int i=1;i<=n;i++)
    {
        minn[color[i]]=0x3f3f3f3f;
    }
    for(int i=1;i<=n;i++)
    {
        if(minn[color[i]]>a[i])
        {
            minn[color[i]]=a[i];
            contz[color[i]]=1;
        }
        else if(minn[color[i]]==a[i])contz[color[i]]++;
    }
    ll ans=0;
    ll ans2=1;
    for(int i=1;i<=sig;i++)
    {
        ans+=minn[i];
        ans2*=contz[i];
        ans2%=mod;
    }
    printf("%I64d %I64d\n",ans,ans2);
}
int main()
{
    while(~scanf("%d",&n))
    {
        init();
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        int m;
        scanf("%d",&m);
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
        }
        Slove();
    }
}










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值