Problem solution · Python

ABC276 E — Round Trip

ABC276 E — Round Trip: a Python solution using disjoint set union. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Disjoint set union
Source
KATO-Hiro AtCoder Solutions
Length
86 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Disjoint set union

For ABC276 E — Round Trip, the implementation maintains connected components and merges them as relationships are processed.

  1. Give each element a component representative.
  2. Merge representatives when a connection is accepted.
  3. Answer connectivity or component queries from the compressed representatives.

Code notes

  • 86 lines of Python from the credited upstream file abc276_e.py.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • No explicit loop blocks detected.

Complexity

Account for every find and union operation; with path compression and ranked merging, the amortized cost is nearly constant per operation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC276 E — Round Trip · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  class UnionFind:    def __init__(self, number_count: int):        self.parent_numbers = [-1 for _ in range(number_count)]     def find_root(self, number: int) -> int:        if self.parent_numbers[number] < 0:            return number         self.parent_numbers[number] = self.find_root(self.parent_numbers[number])        return self.parent_numbers[number]     def merge_if_needs(self, number_x: int, number_y: int) -> bool:        x = self.find_root(number_x)        y = self.find_root(number_y)         if x == y:            return False         if self.parent_numbers[x] > self.parent_numbers[y]:            x, y = y, x         self.parent_numbers[x] += self.parent_numbers[y]        self.parent_numbers[y] = x        return True  def main():    import sys     input = sys.stdin.readline     h, w = map(int, input().split())    s = [list(input().rstrip()) for _ in range(h)]    n = h * w + 1    uf = UnionFind(n)     def to_id(i, j):        return i * w + j     # Sに隣接する4マスのうち異なる2マスを行き来できるか?    # 連結性の判定問題と言い換え    sx, sy = -1, -1     for i in range(h):        for j in range(w):            if s[i][j] == "S":                sx, sy = j, i             if s[i][j] != ".":                continue             # 着目しているマスの右と下のみチェックすれば十分            if (i + 1 < h) and s[i + 1][j] == ".":                uf.merge_if_needs(to_id(i, j), to_id(i + 1, j))            if (j + 1 < w) and s[i][j + 1] == ".":                uf.merge_if_needs(to_id(i, j), to_id(i, j + 1))     # leaderが一つでも一致しているか?    dxy = [(-1, 0), (1, 0), (0, -1), (0, 1)]    count, leaders = 0, set()     for dx, dy in dxy:        nx, ny = sx + dx, sy + dy            if ny < 0 or ny >= h:            continue        if nx < 0 or nx >= w:            continue        if s[ny][nx] == "#":            continue         leaders.add(uf.find_root(to_id(ny, nx)))        count += 1        if len(leaders) == count:        print("No")    else:        print("Yes")  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗