ABC277 回想・解説(Python)

Thumbnail

目次

前提

AtCoder Biginner Contest277のPythonの解答コードです。

A - ^-1

Xのインデックスを返す。

N, X = map(int, input().split())
P = list(map(int, input().split()))

for i, p in enumerate(P, start=1):
    if p == X:
        exit(print(i))

B - Playing Cards Validation

1文字目と2文字目のバリデーションをチェック。通るものをsetで管理。例外が発生したら、回答は"No"。

first = ("H", "D", "C", "S")
second = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K")

d = set()
N = int(input())
ans = "Yes"
for i in range(N):
    s = input()
    if s[0] in first and s[1] in second:
        if s not in d:
            d.add(s)
        else:
            ans = "No"
    else:
        ans = "No"
print(ans)

C - Ladder Takahashi

UnionFind

素直にUnionFindで実装しようとするとA,Bの制約が大きいのでTLEしてしまう。はしごのNはN<=2*105なので、階数を別の数字に変換する。UFの初期化を2*105で済むので、実行時間に間に合う。変換するdictと変換した数字をもとに戻すdictを2つ用意して最終的には階数を返せるようにしておく。

from collections import defaultdict


class UnionFind:
    """Union Find木クラス
    Attributes:
        parent (list): 親要素を格納するリスト。親要素は自身のサイズを格納する。
    Notes:
        - parent[i] < 0 の場合、iは根。この時、-parent[i]はそのグループのサイズ
        - parent[i] = -1 とすると、iは独立。
    """

    def __init__(self, n):
        """
        Args:
            n (int): The number of objects. 要素数
        """
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        """returns the root of x

        Args:
            x (int): The object to find the root of. 要素

        Returns:
            int: The root of x.
        """
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        """unites the root of x and the root of y

        Args:
            x (int): The first object to union. 要素
            y (int): The second object to union. 要素
        """
        x = self.find(x)
        y = self.find(y)
        if x == y:  # すでに同じ親
            return
        # union by size
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        """returns the size of the group of x

        Args:
            x (int): The object to find the size of. 要素

        Returns:
            int: The size of the group of x.
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """returns True if the root of x is the same as the root of y

        Args:
            x (int): The first object to check. 要素
            y (int): The second object to check. 要素

        Returns:
            bool: True if the root of x is the same as the root of y.
        """
        return self.find(x) == self.find(y)

    def members(self, x):
        """returns the members of the group of x

        Args:
            x (int): The object to find the members of. 要素

        Returns:
            list: The members of the group of x.
        """
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        """returns the roots

        Returns:
            list: The roots.
        """
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        """returns the number of groups

        Returns:
            int: The number of groups.
        """
        return len(self.roots())

    def all_group_members(self):
        """returns all groups

        Returns:
            list: All group members.
        """
        return {r: self.members(r) for r in self.roots()}


N = int(input())
uf = UnionFind(2 * 10**5)
toInt = defaultdict(int)
toInt[1] = 1
toLadder = defaultdict(int)
toLadder[1] = 1
cnt = 2
for i in range(N):
    a, b = map(int, input().split())
    if toInt[a] == 0:
        toInt[a] = cnt
        toLadder[cnt] = a
        cnt += 1
    if toInt[b] == 0:
        toInt[b] = cnt
        toLadder[cnt] = b
        cnt += 1
    uf.union(toInt[a], toInt[b])
ans = 1
for i in uf.members(1):
    ans = max(ans, toLadder[i])

print(ans)

BFS

はしごを辺、階数を頂点とみなして、BFSすると到達できる階数を走査できる。

from collections import defaultdict, deque

n = int(input())
d = defaultdict(list)
for _ in range(n):
    a, b = map(int, input().split())
    d[a].append(b)
    d[b].append(a)

q = deque()
q.append(1)
S = {1}
while q:
    v = q.popleft()
    for i in d[v]:
        if i not in S:
            q.append(i)
            S.add(i)
print(max(S))