Rumor (Codeforces 893C)
Tags: Greedy, dfs and similar

Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?Take a look at the notes if you think you haven't understood the problem completely.

Input: The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.

Output: Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.

Note: In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.In the second example Vova has to bribe everyone.In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.

Sample Inputs:
5 2
2 5 3 4 8
1 4
4 5

10 0
1 2 3 4 5 6 7 8 9 10

10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10

Sample Outputs:
10

55

15

Solution (Python):
(n, m) = [int(x) for x in input().split()]
costs = [int(x) for x in input().split()]
costs.insert(0, 0)
parent_for_i = [i for i in range(n + 1)]

def find_parent(i):
    while parent_for_i[i] != i:
        parent_for_i[i] = parent_for_i[parent_for_i[i]]
        i = parent_for_i[i]
    return i
for _ in range(m):
    (i, j) = [int(x) for x in input().split()]
    (i, j) = (find_parent(i), find_parent(j))
    if costs[i] < costs[j]:
        parent_for_i[j] = i
    else:
        parent_for_i[i] = j
print(sum((costs[i] for i in range(n + 1) if parent_for_i[i] == i)))

Solution (Python):
def find(x, p):
    while p[x] != x:
        x = p[x]
    return x

def unite(a, b, p, c, g):
    ar = find(a, p)
    br = find(b, p)
    if c[ar] < c[br]:
        p[ar] = br
    else:
        p[br] = ar
        if c[ar] == c[br]:
            c[ar] += 1

def main():
    (n, m) = map(int, input().split())
    g = [0] + list(map(int, input().split()))
    p = list(range(n + 1))
    c = [1] * (n + 1)
    r = 0
    for _ in range(m):
        (x, y) = map(int, input().split())
        unite(x, y, p, c, g)
    for i in range(1, n + 1):
        j = find(i, p)
        if j != i:
            g[j] = min(g[j], g[i])
    for i in range(1, n + 1):
        if p[i] == i:
            r += g[i]
    print(r)
if __name__ == '__main__':
    main()

Solution (Python):
R = lambda : map(int, input().split())
(n, m) = R()
pa = [i for i in range(n + 1)]
T = [0] + list(R())

def find(i):
    while i != pa[i]:
        pa[i] = pa[pa[i]]
        i = pa[i]
    return i
for _ in range(m):
    (x, y) = map(find, R())
    if T[x] < T[y]:
        (x, y) = (y, x)
    pa[x] = y
print(sum((T[i] for i in range(n + 1) if pa[i] == i)))