From 3487bfc0ab9db513fedf7cc6e060e26f745638a1 Mon Sep 17 00:00:00 2001 From: yenru0 Date: Thu, 16 May 2024 23:51:27 +0900 Subject: [PATCH] complete 1325.py and 2470.py --- zeta_python/completed/1325.py | 37 +++++++++++++++++++++++++++++++++++ zeta_python/completed/2470.py | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 zeta_python/completed/1325.py create mode 100644 zeta_python/completed/2470.py diff --git a/zeta_python/completed/1325.py b/zeta_python/completed/1325.py new file mode 100644 index 0000000..f2ce0ee --- /dev/null +++ b/zeta_python/completed/1325.py @@ -0,0 +1,37 @@ +from collections import deque +import sys + +input = sys.stdin.readline + + +def bfs(N, H, start): + Vsx = [0] * (N + 1) + count = 0 + D = deque() + D.append(start) + Vsx[start] = 1 + + while D: + now = D.pop() + count += 1 + for target in H[now]: + if not Vsx[target]: + D.append(target) + Vsx[target] = 1 + + return count + + +def solve(N, M, H): + S = [bfs(N, H, i) for i in range(1, N + 1)] + m = max(S) + return [i + 1 for i, v in enumerate(S) if v == m] + + +if __name__ == "__main__": + N, M = map(int, input().split()) + H = [[] for _ in range(N + 1)] + for _ in range(M): + s, e = map(int, input().split()) + H[e].append(s) + print(*solve(N, M, H)) diff --git a/zeta_python/completed/2470.py b/zeta_python/completed/2470.py new file mode 100644 index 0000000..953ac6e --- /dev/null +++ b/zeta_python/completed/2470.py @@ -0,0 +1,37 @@ +def solve_simple(N, S): + _, s = min( + [ + (abs(S[i] + S[j]), sorted((S[i], S[j]))) + for i in range(N) + for j in range(N) + if i != j + ] + ) + return s + + +def solve(N: int, S: list) -> tuple[int, int]: + S.sort() + start = 0 + end = N - 1 + r = 10000000000 + r_pos = None, None + while start < end: + s = S[start] + S[end] + if r > abs(s): + r = abs(s) + r_pos = start, end + if r == 0: + break + if s <= 0: + start += 1 + elif s > 0: + end -= 1 + + return S[r_pos[0]], S[r_pos[1]] + + +if __name__ == "__main__": + N = int(input()) + S = list(map(int, input().split())) + print(*solve(N, S))