結果
| 問題 |
No.1787 Do Use Dynamic Tree
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 13:47:39 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,533 bytes |
| コンパイル時間 | 332 ms |
| コンパイル使用メモリ | 82,668 KB |
| 実行使用メモリ | 160,620 KB |
| 最終ジャッジ日時 | 2025-06-12 13:48:17 |
| 合計ジャッジ時間 | 18,365 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 26 TLE * 1 -- * 11 |
ソースコード
import sys
from sys import stdin
sys.setrecursionlimit(1 << 25)
def main():
input = sys.stdin.read().split()
ptr = 0
N = int(input[ptr])
ptr +=1
adj = [[] for _ in range(N+1)] # 1-based
for _ in range(N-1):
a = int(input[ptr])
b = int(input[ptr+1])
ptr +=2
adj[a].append(b)
adj[b].append(a)
Q = int(input[ptr])
ptr +=1
queries = []
for _ in range(Q):
u = int(input[ptr])
v = int(input[ptr+1])
ptr +=2
queries.append((u, v))
# Initial values
val = list(range(N+1)) # val[1] = 1, etc.
x = 0
for (ui_orig, vi_orig) in queries:
# Decrypt u and v
u = ((ui_orig + N -1 + x) % N) +1
v = ((vi_orig + N -1 + x) % N) +1
# Swap val[u] and val[v]
val[u], val[v] = val[v], val[u]
# Simulate the greedy path from u
current = u
prev_node = -1
while True:
next_node = -1
max_val = -1
for neighbor in adj[current]:
if neighbor == prev_node:
continue
if val[neighbor] > max_val or (val[neighbor] == max_val and neighbor > next_node):
max_val = val[neighbor]
next_node = neighbor
if next_node == -1:
break
prev_node = current
current = next_node
ans = current
print(ans)
x = ans
if __name__ == '__main__':
main()
gew1fw