結果
| 問題 | No.399 動的な領主 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-05-04 21:35:08 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,712 bytes |
| コンパイル時間 | 326 ms |
| コンパイル使用メモリ | 82,048 KB |
| 実行使用メモリ | 277,076 KB |
| 最終ジャッジ日時 | 2024-11-26 11:56:12 |
| 合計ジャッジ時間 | 41,943 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 6 TLE * 13 |
ソースコード
import sys
input = sys.stdin.read
from collections import defaultdict, deque
def main():
data = input().split()
index = 0
n = int(data[index])
index += 1
tree = defaultdict(list)
for _ in range(n - 1):
u = int(data[index]) - 1
v = int(data[index + 1]) - 1
tree[u].append(v)
tree[v].append(u)
index += 2
m = int(data[index])
index += 1
routes = []
for _ in range(m):
s = int(data[index]) - 1
t = int(data[index + 1]) - 1
routes.append((s, t))
index += 2
# 村を通過する行商人の数をカウントする
village_count = [0] * n
def bfs_count_passes(start, end):
queue = deque([start])
visited = [False] * n
visited[start] = True
parents = [-1] * n
# BFS to find the path from start to end
while queue:
node = queue.popleft()
if node == end:
break
for neighbor in tree[node]:
if not visited[neighbor]:
visited[neighbor] = True
parents[neighbor] = node
queue.append(neighbor)
# Backtrack to count the path
node = end
while node != -1:
village_count[node] += 1
node = parents[node]
# 各ルートについてBFSを実行
for s, t in routes:
bfs_count_passes(s, t)
# 税額の総計を計算
total_tax = 0
for i in range(n):
if village_count[i] > 0:
total_tax += sum(range(village_count[i] + 1))
print(total_tax)
if __name__ == "__main__":
main()