Codeforces Round #244 (Div. 2) C. Checkposts

题目

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

Sample test(s)

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

解析

 图的强连通分支查找,可以使用双向遍历的方法求解,也可使用tarjan方法求解,这里选择tarjan

答案

#include <cstring>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
#define CLR(vertex, value)          memset(vertex, value, sizeof(vertex))
/*used for solve*/
#define MAX_CHECKPOST 101000
#define MAX_WAY       301000
#define MAX_COST      ((int)( (~(unsigned int)0) >> 1))
#define MOD           1000000007
vector<int> graph[MAX_CHECKPOST];            /*record the graph*/
int cost_table[MAX_CHECKPOST];               /*checkpost buildcost*/
int tot_checkpost;                           /*tot checkpost*/
int tot_way;                                 /*tot way*/
/*store info for strong connected*/
//int strongconnected_table[MAX_CHECKPOST];  /*strongconnected_table id*/  
int min_value[MAX_CHECKPOST];                /*min cost in this strong connected*/
int min_cnt[MAX_CHECKPOST];                  /*tot pos in min value in this strongconnected*/  
/*used for tarjan*/
int dfs_clock, low[MAX_CHECKPOST], dfn[MAX_CHECKPOST], strongconnected_group;
bool in_stack[MAX_CHECKPOST]; 
stack<int> pos_stack;

static void tarjan(int curr_pos);
static void init_tarjan();
static void run_tarjan();
static void read_input();
static void build_output();
/**
 * @brief tarjan
 * @param[in] curr_pos in graph
 *
 */
void tarjan(int curr_pos){
    int next_pos;
    dfn[curr_pos] = low[curr_pos] = dfs_clock++;
    pos_stack.push(curr_pos);
    in_stack[curr_pos] = true;
    for(int way_id = 0; way_id < graph[curr_pos].size(); way_id++){
            next_pos = graph[curr_pos][way_id];
            if(!dfn[next_pos]){                             /*first visit*/
                    tarjan(next_pos);
                    low[curr_pos] = min(low[curr_pos], low[next_pos]);
            }else if(in_stack[next_pos]){                   /*is in stack*/
                    low[curr_pos] = min(low[curr_pos], dfn[next_pos]);
            }
    }
    if(low[curr_pos] == dfn[curr_pos]){
        int strongconnected_root =  curr_pos;
        int strongconnected_child;
        strongconnected_group++;
        do{
            strongconnected_child = pos_stack.top();    
            //strongconnected_table[strongconnected_child] = strongconnected_group;
            pos_stack.pop();
            in_stack[strongconnected_child] = false;
            
            if(cost_table[strongconnected_child] < min_value[strongconnected_group]){
                    min_value[strongconnected_group] = cost_table[strongconnected_child];        
                    min_cnt[strongconnected_group]   = 1;
            }else if( cost_table[strongconnected_child] == min_value[strongconnected_group]){
                    min_cnt[strongconnected_group]++;
            }
#if 0
            cout << " strongconnected_group = " << strongconnected_group << endl;
            cout << " strongconnected_child = " << strongconnected_child << endl;
            cout << " min_value = "             << min_value[strongconnected_group]  << endl;
            cout << " min_cnt = "               << min_cnt[strongconnected_group]  << endl;
            cout << endl;
#endif
        }while( strongconnected_child != strongconnected_root);
    }
}

/**
 * @brief init for tarjan
 *
 */
void init_tarjan(){
    dfs_clock = 1;
    strongconnected_group = 0;
    for(int curr_checkpost = 1; curr_checkpost <= tot_checkpost; curr_checkpost++){
            min_value[curr_checkpost] = MAX_COST;
    }
    CLR(in_stack, false);
}

/**
 * @brief run tarjan
 * @param[in] curr_pos in graph
 *
 */
void run_tarjan(){
    int curr_checkpost;
    init_tarjan();
    for(curr_checkpost = 1; curr_checkpost <= tot_checkpost; curr_checkpost++){
        if(!dfn[curr_checkpost]){
            tarjan(curr_checkpost);
        }
    }
}
/**
 * @brief build output by tarjan
 *
 */
void build_output()
{
    long long int totvalue = 0;
    long long int totpos   = 1;
    int sc_id;
    for(sc_id = 1; sc_id <= strongconnected_group; sc_id++){
#if 0
        cout << "sc_id = "   << sc_id << endl;
        cout << "min_cnt = " << min_cnt[sc_id] << endl;
#endif
        totvalue += min_value[sc_id];       
        totpos   *= min_cnt[sc_id];
        if(totpos > MOD){
            totpos %= MOD;
        }
    }
    cout << totvalue << " " << totpos << endl;
}
/**
 * @brief read input for problem
 *
 */
void read_input()
{
    int curr_checkpost, curr_way, from_pos, to_pos;
    cin >> tot_checkpost;
    for(curr_checkpost = 1; curr_checkpost <= tot_checkpost; curr_checkpost++){  /*record all cost*/
                cin >> cost_table[curr_checkpost];
    }
    cin >> tot_way;
    for(curr_way = 1; curr_way <= tot_way; curr_way++){                       /*build graph*/
        cin >> from_pos;
        cin >> to_pos;
        graph[from_pos].push_back(to_pos);
    }
}

int main()
{
    read_input();
    run_tarjan();
    build_output();
return 0;
}




转载于:https://my.oschina.net/u/572632/blog/278486

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值