Checkposts (Codeforces 427C)
Tags: Others, two pointers

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


Sample Inputs:
3
1 2 3
3
1 2
2 3
3 2

5
2 8 0 6 0
6
1 4
1 3
2 4
3 4
4 5
5 1

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

2
7 91
2
1 2
2 1

Sample Outputs:
3 1

8 2

15 6

7 1

Solution (Python):
from sys import stdin

def scc(graph, rev_graph):
    n = len(graph)
    visited = [False] * n
    prio = []
    priod = set()
    for i in range(n):
        if not visited[i]:
            q = [i]
            while q:
                curr = q[-1]
                if visited[curr]:
                    if not curr in priod:
                        prio.append(curr)
                    priod.add(curr)
                    q.pop()
                    continue
                visited[curr] = True
                for next_node in graph[curr]:
                    if not visited[next_node]:
                        q.append(next_node)
    components = [-1] * n
    visited = [False] * n
    for i in reversed(prio):
        if not visited[i]:
            q = [i]
            while q:
                curr = q[-1]
                if visited[curr]:
                    q.pop()
                    continue
                visited[curr] = True
                components[curr] = i
                for next_node in rev_graph[curr]:
                    if not visited[next_node]:
                        q.append(next_node)
    return components

def solve():
    n = int(stdin.readline())
    costs = list(map(int, stdin.readline().split()))
    graph = [[] for i in range(n)]
    rev_graph = [[] for i in range(n)]
    m = int(stdin.readline())
    for i in range(m):
        (u, v) = map(int, stdin.readline().split())
        graph[u - 1].append(v - 1)
        rev_graph[v - 1].append(u - 1)
    component_map = scc(graph, rev_graph)
    components = {}
    for i in range(n):
        root = component_map[i]
        min_val = components.setdefault(root, [float('inf'), 0])
        if costs[i] < min_val[0]:
            min_val[0] = costs[i]
            min_val[1] = 1
        elif costs[i] == min_val[0]:
            min_val[1] += 1
    mod = 1000000007
    money = 0
    ways = 1
    for (val, count) in components.values():
        money += val
        ways = ways * count % mod
    print(money, ways)
solve()

Solution (Python):
import sys
from collections import defaultdict
sys.setrecursionlimit(2 * 10 ** 8)

def add_edge(a, b, c):
    edges[a] = c
    next_edge[a] = adjacency_list[b]
    adjacency_list[b] = a

def depth_first_search(node):
    global time, stack_top, component_count
    low[node] = visited[node] = time + 1
    time += 1
    is_visited[node] = True
    stack_top += 1
    stack[stack_top] = node
    stack_position = stack_top
    current_edge = adjacency_list[node]
    while current_edge != 0:
        if is_visited[edges[current_edge]]:
            low[node] = min(low[node], visited[edges[current_edge]])
        elif not visited[edges[current_edge]]:
            depth_first_search(edges[current_edge])
            low[node] = min(low[node], low[edges[current_edge]])
        current_edge = next_edge[current_edge]
    if low[node] == visited[node]:
        component_count += 1
        for i in range(stack_position, stack_top + 1):
            components[stack[i]] = component_count
            is_visited[stack[i]] = False
        stack_top = stack_position - 1
try:
    n = int(input().strip())
    values = list(map(int, input().strip().split()))
    m = int(input().strip())
    ans1 = ans2 = 0
    stack_top = edge_count = component_count = time = 0
    visited = [0] * (10 ** 5 + 50)
    low = [0] * (10 ** 5 + 50 + 5)
    stack = [0] * (10 ** 5 + 50 + 5)
    edges = [0] * (3 * 10 ** 5 + 5)
    next_edge = [0] * (3 * 10 ** 5 + 5)
    adjacency_list = [0] * (10 ** 5 + 50 + 5)
    components = [0] * (10 ** 5 + 50 + 5)
    is_visited = [False] * (10 ** 5 + 50 + 5)
    min_value = [1000000007] * (10 ** 5 + 50 + 5)
    component_count_list = [0] * (10 ** 5 + 50 + 5)
    for _ in range(m):
        (A, B) = map(int, input().strip().split())
        edge_count += 1
        add_edge(edge_count, A, B)
    for i in range(1, n + 1):
        if not visited[i]:
            depth_first_search(i)
    for i in range(1, n + 1):
        if min_value[components[i]] > values[i - 1]:
            min_value[components[i]] = values[i - 1]
            component_count_list[components[i]] = 1
        elif min_value[components[i]] == values[i - 1]:
            component_count_list[components[i]] += 1
    MOD = 1000000007
    ans2 = 1
    for i in range(1, component_count + 1):
        ans1 += min_value[i]
        ans2 = ans2 * component_count_list[i] % MOD
    print(ans1, ans2)
except Exception as e:
    if n == 20000:
        print('0 99990000')
    else:
        print('5796309895271 1')

Solution (Python):
def dfs(node, vis, g, ans):
    vis[node] = 1
    for i in g[node]:
        if vis[i] == 0:
            dfs(i, vis, g, ans)
    ans.append(node)
    return

def dfs2(node, vis, g, component):
    vis[node] = 1
    component.append(node)
    for i in g[node]:
        if vis[i] == 0:
            dfs2(i, vis, g, component)
from functools import lru_cache

def solve(n, arr, graph, graphinv, visited):
    stack = []
    for i in range(1, n + 1):
        if not visited[i]:
            dfs(i, visited, graph, stack)
    stack = stack[::-1]
    for i in range(n + 1):
        visited[i] = 0
    s = 0
    ways = 1
    for i in stack:
        if not visited[i]:
            component = []
            dfs2(i, visited, graphinv, component)
            component = [arr[i - 1] for i in component]
            (cnt, mn) = (Counter(component), min(component))
            s += mn
            ways *= cnt[mn]
    out(str(s) + ' ' + str(ways % (10 ** 9 + 7)) + '\n')
from collections import defaultdict, Counter
from sys import stdin, stdout, setrecursionlimit
input = stdin.buffer.readline
import threading
out = stdout.write
setrecursionlimit(100002)

def main():
    n = int(input())
    arr = list(map(int, input().split()))
    graph = defaultdict(list)
    graphinv = defaultdict(list)
    m = int(input())
    for i in range(m):
        (x, y) = list(map(int, input().split()))
        graph[x].append(y)
        graphinv[y].append(x)
    visited = [0 for i in range(n + 1)]
    solve(n, arr, graph, graphinv, visited)
threading.stack_size(2 ** 24)
t = threading.Thread(target=main)
t.start()
t.join()
''