def test_all(f):
for i, data in enumerate(eval(f'test_data{f.__name__[0]}')):
exp = data["out"]
ans = f(*data["in"])
result = "AC" if exp == ans else "WA"
print(f"{i+1} {result}: expected: {exp}, output: {ans}")
def B(X, Y, Z):
if abs(X) > abs(Y) and X * Y > 0:
if Y * Z > 0:
if abs(Y) < abs(Z):
return -1
else:
return abs(X)
else:
return abs(Z) * 2 + abs(X)
else:
return abs(X)
test_all(B)
def B(X, Y, Z):
if abs(X) < abs(Y) or X * Y < 0:
return abs(X)
else:
if Y * Z < 0:
return abs(X - 2 * Z)
elif abs(Z) > abs(Y):
return -1
else:
return abs(X)
test_all(B)
from collections import deque
def C(N, X, Y, UV):
edge = [[] for _ in range(N+1)]
for u, v in UV:
edge[u].append(v)
edge[v].append(u)
route = [None] * (N + 1)
q = deque([X])
while len(q):
now = q.popleft()
for n in edge[now]:
if route[n] is None:
route[n] = now
if n == Y: break
q.append(n)
else:
continue
break
ans = [Y]
now = Y
while now != X:
now = route[now]
ans.append(now)
return " ".join(map(str, ans[::-1]))test_all(C)
from collections import deque
def C(N, X, Y, UV):
flag = [False] * (N + 1)
s = []
stop = False
def dfs(k, to):
nonlocal stop
if stop: return
else: s.append(k)
if k == to:
stop = True
return
flag[k] = True
for i in edge[k]:
if not flag[i]: dfs(i, to)
if not stop: s.pop()
edge = [[] for _ in range(N+1)]
for u, v in UV:
edge[u].append(v)
edge[v].append(u)
dfs(X, Y)
import io
out = io.StringIO()
print(*s, file=out, end="")
return out.getvalue()test_all(C)
from collections import deque
import sys
sys.setrecursionlimit(2 * 10 ** 6)
route = []
def C(N, X, Y, UV):
def dfs(n):
route.append(n)
if n == Y:
print(*route)
# exit() # 提出用
for nxt in edge[n]:
if len(route) >= 2 and nxt == route[-2]:
continue
else:
dfs(nxt)
route.pop()
edge = [[] for _ in range(N+1)]
for u, v in UV:
edge[u].append(v)
edge[v].append(u)
dfs(X)test_all(C)
def C(N, X, Y, UV):
edge = [[] for _ in range(N+1)]
for u, v in UV:
edge[u].append(v)
edge[v].append(u)
route = [None] * (N + 1)
q = [X]
while len(q):
now = q.pop()
for n in edge[now]:
if route[n] is None:
route[n] = now
if n == Y: break
q.append(n)
else:
continue
break
ans = [Y]
now = Y
while now != X:
now = route[now]
ans.append(now)
return " ".join(map(str, ans[::-1]))test_all(C)
from bisect import bisect
def D(N, K, A):
ans = 0
i = 0
while N >= A[0]:
get = A[bisect(A, N) - 1]
if i % 2 == 0: ans += get
N -= get
i += 1
return anstest_all(D)