Problem p02789 (python)

Fixed submission:
N, M = map(int, input().split())

print("Yes" if N == M else "No")

Buggy submission (verdict: Runtime Error):
def factorization(n) -> list:
    arr = []
    temp = n
    for i in range(2, int(- (- n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])
        if temp != 1:
            arr.append([temp, 1])
        if arr == []:
            arr.append([n, 1])
        return arr
      
print(factrization(24))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.buffer.readline

N = int(input())
AB = [list(map(int, input().split)) for _ in range(N - 1)]

graph = [[] for _ in range(N + 1)]
for a, b in AB:
    graph[a].append(b)
    graph[b].append(a)
  
def make_tree(graph, root) -> (list, list, list):
    INF = 10 ** 15
    dist = [INF] * len(graph)
    parent = [-1] * len(graph)
    stack = [root]
    dist[root] = 0
    order = []
    parent[root] = root
    while stack:
        s = stack.pop()
        order.append(s)
        for g in graph[s]:
            if dist[g] > dist[s] + 1:
                dist[g] = dist[s] + 1
                parent[g] = s
                stack.append(g)
    return dist, parent, order
  
def lca(u, v, dist, parent):
    distu, distv = dist[u], dist[v]
    if distu > distv:
        for _ in range(distu - distv):
            u = parent[u]
    if distv > distu:
        for _ in range(distv - distu):
            v = parent[v]
    while u != v:
        u, v = parent[u], parent[v]
    return u

Buggy submission (verdict: Runtime Error):
graph = [[] for _ in range(N + 1)]
for a, b in AB:
  graph[a].append(b)
  graph[b].append(a)
  
def make_tree(graph, root) -> (list, list):
  INF = 10 ** 15
  dist = [INF] * len(graph)
  parent = [-1] * len(graph)
  stack = [root]
  dist[root] = 0
  parent[root] = root
  while stack:
    s = stack.pop()
    for g in graph[s]:
      if dist[g] > dist[s] + 1:
        dist[g] = dist[s] + 1
        parent[g] = s
        stack.append(g)
  return dist, parent
 
def lca(u, v, dist, parent):
  distu, distv = dist[u], dist[v]
  if distu > distv:
    for _ in range(distu - distv):
      u = parent[u]
  if distv > distu:
    for _ in range(distv - distu):
      v = parent[v]
  while u != v:
    u, v = parent[u], parent[v]
  return u

#root = 1
#dist, parent = make_tree(graph, root)
#print("dist:", dist[1:])
#print("parent:", parent[1:])

Buggy submission (verdict: Runtime Error):
graph = [[] for _ in range(N + 1)]
for a, b in AB:
  graph[a].append(b)
  graph[b].append(a)
  
def make_tree(graph, root) -> (list, list):
  INF = 10 ** 15
  dist = [INF] * len(graph)
  parent = [-1] * len(graph)
  stack = [root]
  dist[root] = 0
  parent[root] = root
  while stack:
    s = stack.pop()
    for g in graph[s]:
      if dist[g] > dist[s] + 1:
        dist[g] = dist[s] + 1
        parent[g] = s
        stack.append(g)
  return dist, parent
 
def lca(u, v, dist, parent):
  distu, distv = dist[u], dist[v]
  if distu > distv:
    for _ in range(distu - distv):
      u = parent[u]
  if distv > distu:
    for _ in range(distv - distu):
      v = parent[v]
  while u != v:
    u, v = parent[u], parent[v]
  return u

# root = 1
# dist, parent = make_tree(graph, root)
# print("dist:", dist[1:])
# print("parent:", parent[1:])

Buggy submission (verdict: Runtime Error):
graph = [[] for _ in range(N + 1)]
for a, b in AB:
  graph[a].append(b)
  graph[b].append(a)
  
def make_tree(graph, root) -> list, list:
  INF = 10 ** 15
  dist = [INF] * len(graph)
  parent = [-1] * len(graph)
  stack = [root]
  dist[root] = 0
  parent[root] = root
  while stack:
    s = stack.pop()
    for g in graph[s]:
      if dist[g] > dist[s] + 1:
        dist[g] = dist[s] + 1
        parent[g] = s
        stack.append(g)
  return dist, parent
 
def lca(u, v, dist, parent):
  distu, distv = dist[u], dist[v]
  if distu > distv:
    for _ in range(distu - distv):
      u = parent[u]
  if distv > distu:
    for _ in range(distv - distu):
      v = parent[v]
  while u != v:
    u, v = parent[u], parent[v]
  return u

# root = 1
# dist, parent = make_tree(graph, root)

Buggy submission (verdict: Runtime Error):
# make_tree(graph, root) -> dist, parent

# LCA: Last Common Ancestor (最近共通祖先)
# lca(u, v, dist, parent)

# Inclusion-exclusion principle: 包除原理
# http://satanic0258.hatenablog.com/entry/2016/04/10/104524
# A∨B∨C = ￢(￢A￢B∨￢C)

N = int(input())
AB = [list(map(int, input().split())) for _ in range(N - 1)]
M = int(input())
UV = [list(map(int, input().split())) for _ in range(M)]

MOD = 10 ** 9 + 7

graph = [[] for _ in range(N + 1)]
for a, b in AB:
  graph[a].append(b)
  graph[b].append(a)
  
def make_tree(graph, root):
  INF = 10 ** 15
  dist = [INF] * len(graph)
  parent = [-1] * len(graph)
  stack = [root]
  dist[root] = 0
  parent[root] = root
  while stack:
    s = stack.pop()
    for g in graph[s]:
      if dist[g] > dist[s] + 1:
        dist[g] = dist[s] + 1
        parent[g] = s
        stack.append(g)
  return dist, parent

def lca(u, v, dist, parent):
  distu, distv = dist[u], dist[v]
  if distu > distv:
    for _ in range(distu - distv):
      u = parent[u]
  if distv > distu:
    for _ in range(distv - distu):
      v = parent[v]
  while u != v:
    u, v = parent[u], parent[v]
  return u

dist, parent = make_tree(graph, 1)
#print(parent)

for i, (u, v) in enumerate(UV):
  UV[i].append(lca(u, v, dist, parent))

cnt = [0] * (N + 1)
for u, v, l in UV:
  tmp = [1] * (N + 1)
  uu, vv = u, v
  while uu != l:
    tmp[uu] = 0
    uu = parent[uu]
  while vv != l:
    tmp[vv] = 0
    vv = parent[vv]
  for i in range(2, N + 1):
    cnt[i] += tmp[i]
print(cnt)

cnt_tmp = 0
for i in range(2, N + 1):
  if cnt[i]:
    cnt_tmp += 1
print((pow(2 , N - 1, MOD) - pow(2, cnt_tmp, MOD)) % MOD)