Ski Base (Codeforces 91C)
Tags: Others, dsu

A ski base is planned to be built in Walrusland. Recently, however, the project is still in the constructing phase. A large land lot was chosen for the construction. It contains n ski junctions, numbered from 1 to n. Initially the junctions aren't connected in any way.In the constructing process m bidirectional ski roads will be built. The roads are built one after another: first the road number 1 will be built, then the road number 2, and so on. The i-th road connects the junctions with numbers ai and bi.Track is the route with the following properties:  The route is closed, that is, it begins and ends in one and the same junction. The route contains at least one road.  The route doesn't go on one road more than once, however it can visit any junction any number of times. Let's consider the ski base as a non-empty set of roads that can be divided into one or more tracks so that exactly one track went along each road of the chosen set. Besides, each track can consist only of roads from the chosen set. Ski base doesn't have to be connected.Two ski bases are considered different if they consist of different road sets.After building each new road the Walrusland government wants to know the number of variants of choosing a ski base based on some subset of the already built roads. The government asks you to help them solve the given problem.

Input: The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). They represent the number of junctions and the number of roads correspondingly. Then on m lines follows the description of the roads in the order in which they were built. Each road is described by a pair of integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers of the connected junctions. There could be more than one road between a pair of junctions.

Output: Print m lines: the i-th line should represent the number of ways to build a ski base after the end of construction of the road number i. The numbers should be printed modulo 1000000009 (109 + 9).

Note: Let us have 3 junctions and 4 roads between the junctions have already been built (as after building all the roads in the sample): 1 and 3, 2 and 3, 2 roads between junctions 1 and 2. The land lot for the construction will look like this:   The land lot for the construction will look in the following way:   We can choose a subset of roads in three ways:   In the first and the second ways you can choose one path, for example, 1 - 2 - 3 - 1. In the first case you can choose one path 1 - 2 - 1.

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

Sample Outputs:
0
0
1
3

Solution (Python):
import sys
input = sys.stdin.readline

def getp(a):
    if P[a] == a:
        return a
    P[a] = getp(P[a])
    return P[a]

def solve():
    global P
    (n, m) = map(int, input().split())
    P = list(range(n + 1))
    MOD = int(1000000000.0 + 9)
    r = 0
    for i in range(m):
        (a, b) = map(int, input().split())
        a = getp(a)
        b = getp(b)
        if a == b:
            r += 1
        else:
            P[a] = b
        print((pow(2, r, MOD) - 1) % MOD)
solve()

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def get_root(s):
    v = []
    while not s == root[s]:
        v.append(s)
        s = root[s]
    for i in v:
        root[i] = s
    return s

def unite(s, t):
    (rs, rt) = (get_root(s), get_root(t))
    if not rs ^ rt:
        return
    if rank[s] == rank[t]:
        rank[rs] += 1
    if rank[s] >= rank[t]:
        root[rt] = rs
        size[rs] += size[rt]
    else:
        root[rs] = rt
        size[rt] += size[rs]
    return

def same(s, t):
    return True if get_root(s) == get_root(t) else False
(n, m) = map(int, input().split())
mod = pow(10, 9) + 9
root = [i for i in range(n + 1)]
rank = [1 for _ in range(n + 1)]
size = [1 for _ in range(n + 1)]
ans = []
ans0 = 0
for _ in range(m):
    (a, b) = map(int, input().split())
    if not same(a, b):
        unite(a, b)
    else:
        ans0 *= 2
        ans0 += 1
        ans0 %= mod
    ans.append(ans0)
sys.stdout.write('\n'.join(map(str, ans)))

Solution (Python):
(n, m) = map(int, input().split())
(p, r) = (list(range(n + 1)), [0] * (n + 1))

def find(x):
    if p[x] != x:
        p[x] = find(p[x])
    return p[x]

def union(x, y):
    (x, y) = (find(x), find(y))
    if x == y:
        return 0
    if r[x] < r[y]:
        (x, y) = (y, x)
    p[y] = x
    r[x] += r[x] == r[y]
    return 1
MOD = 10 ** 9 + 9
ans = 1
out = []
for _ in range(m):
    (u, v) = map(int, input().split())
    if not union(u, v):
        ans = (ans << 1) % MOD
    out.append((ans + MOD - 1) % MOD)
print(*out, sep='\n')