

def numIslands2(self, n, m, operators):
    '\n        :type n: int\n        :type m: int\n        :type operators: list[Point]\n        :rtype: list[int]\n        '
    rows = n
    cols = m
    unroll = (lambda x, y: ((x * cols) + y))
    mat = [[0 for _ in xrange(cols)] for _ in xrange(rows)]
    uf = UnionFind(rows, cols)
    ret = []
    for op in operators:
        uf.add(unroll(op.x, op.y))
        mat[op.x][op.y] = 1
        for dir in self.dirs:
            x1 = (op.x + dir[0])
            y1 = (op.y + dir[1])
            if ((0 <= x1 < rows) and (0 <= y1 < cols) and (mat[x1][y1] == 1)):
                uf.union(unroll(op.x, op.y), unroll(x1, y1))
        ret.append(uf.count)
    return ret

dataset/ETHPy150Open algorhythms/LintCode/Number of Islands II.py Solution.numIslands2/original