ABC解説

東京海上日動プログラミングコンテスト2023(AtCoder Beginner Contest 304)

A. First Player

import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    al = []
    sl = []
    for i in range(n):
        s, a = list(map(str, str(input().rstrip('\n')).split()))
        al.append(int(a))
        sl.append(s)
    st = al.index(min(al))
    for i in range(n):
        print(sl[(i+st) % n])


if __name__ == '__main__':
    solve()

B. Subscribers

import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = list(str(input().rstrip('\n')))
    for i in range(3, len(n)):
        n[i] = "0"
    print("".join(n))


if __name__ == '__main__':
    solve()

C. Virus

import sys
import collections


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, d = list(map(int, input().rstrip('\n').split()))
    xy = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    dq = collections.deque([0])
    fq = collections.defaultdict(int)
    fq[0]
    while len(dq):
        t = dq.popleft()
        for i in range(n):
            if i != t:
                u = pow(pow(xy[t][0] - xy[i][0], 2) + pow(xy[t][1] - xy[i][1], 2), 0.5)
                if u <= d:
                    if i not in fq:
                        dq.append(i)
                        fq[i]
    for i in range(n):
        if i in fq:
            print("Yes")
        else:
            print("No")


if __name__ == '__main__':
    solve()

D. A Piece of Cake

import collections
import sys
import bisect


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    w, h = list(map(int, input().rstrip('\n').split()))
    n = int(input().rstrip('\n'))
    pq = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    a = int(input().rstrip('\n'))
    al = list(map(int, input().rstrip('\n').split()))
    b = int(input().rstrip('\n'))
    bl = list(map(int, input().rstrip('\n').split()))
    d = collections.defaultdict(int)
    for i in range(n):
        p, q = pq[i]
        a_pos = bisect.bisect_left(al, p)
        b_pos = bisect.bisect_left(bl, q)
        d[a_pos, b_pos] += 1
    mx = 0
    mn = pow(10, 30)
    for k, v in d.items():
        mx = max(mx, v)
        mn = min(mn, v)
    mn = 0 if len(d) != (a + 1) * (b + 1) else mn
    print(mn, mx)


if __name__ == '__main__':
    solve()

E. Good Graph

import sys
import collections


class AlgUnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, 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):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        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):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, m = list(map(int, input().rstrip('\n').split()))
    uf = AlgUnionFind(n)
    for i in range(m):
        v1, v2 = list(map(int, input().rstrip('\n').split()))
        uf.union(v1-1, v2-1)
    k = int(input().rstrip('\n'))
    d = collections.defaultdict(int)
    for i in range(k):
        x, y = list(map(int, input().rstrip('\n').split()))
        x, y = uf.find(x-1), uf.find(y-1)
        if x > y:
            x, y = y, x
        d[x, y]
    q = int(input().rstrip('\n'))
    for i in range(q):
        p, q = list(map(int, input().rstrip('\n').split()))
        p, q = uf.find(p-1), uf.find(q-1)
        if p > q:
            p, q = q, p
        if (p, q) in d:
            print("No")
        else:
            print("Yes")


if __name__ == '__main__':
    print(pow(3, 2023) * pow(7, 604))
    solve()

-ABC解説