結果
問題 | No.901 K-ary εxtrεεmε |
ユーザー | chineristAC |
提出日時 | 2020-09-27 22:41:16 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 862 ms / 3,000 ms |
コード長 | 2,091 bytes |
コンパイル時間 | 271 ms |
コンパイル使用メモリ | 82,468 KB |
実行使用メモリ | 205,820 KB |
最終ジャッジ日時 | 2024-06-30 14:05:03 |
合計ジャッジ時間 | 21,505 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 455 ms
205,820 KB |
testcase_01 | AC | 41 ms
55,304 KB |
testcase_02 | AC | 59 ms
67,796 KB |
testcase_03 | AC | 59 ms
68,712 KB |
testcase_04 | AC | 59 ms
67,856 KB |
testcase_05 | AC | 59 ms
68,504 KB |
testcase_06 | AC | 59 ms
69,228 KB |
testcase_07 | AC | 810 ms
174,448 KB |
testcase_08 | AC | 806 ms
175,212 KB |
testcase_09 | AC | 758 ms
167,388 KB |
testcase_10 | AC | 815 ms
173,764 KB |
testcase_11 | AC | 799 ms
175,240 KB |
testcase_12 | AC | 756 ms
163,512 KB |
testcase_13 | AC | 792 ms
166,388 KB |
testcase_14 | AC | 785 ms
161,452 KB |
testcase_15 | AC | 763 ms
162,512 KB |
testcase_16 | AC | 800 ms
161,796 KB |
testcase_17 | AC | 817 ms
177,296 KB |
testcase_18 | AC | 835 ms
179,532 KB |
testcase_19 | AC | 844 ms
179,224 KB |
testcase_20 | AC | 816 ms
175,992 KB |
testcase_21 | AC | 816 ms
178,408 KB |
testcase_22 | AC | 714 ms
157,908 KB |
testcase_23 | AC | 747 ms
161,724 KB |
testcase_24 | AC | 735 ms
160,372 KB |
testcase_25 | AC | 719 ms
159,196 KB |
testcase_26 | AC | 767 ms
164,916 KB |
testcase_27 | AC | 814 ms
182,632 KB |
testcase_28 | AC | 849 ms
181,828 KB |
testcase_29 | AC | 862 ms
182,836 KB |
ソースコード
import sys from collections import deque input = sys.stdin.readline sys.setrecursionlimit(2*10**5) N = int(input()) edge = [[] for i in range(N)] for i in range(N-1): u,v,w = map(int,input().split()) edge[u].append((v,w)) edge[v].append((u,w)) color_tree_edge = [set([]) for i in range(N)] Q = int(input()) color = [list(map(int,input().split()))[1:] for i in range(Q)] for i in range(Q): for v in color[i]: color_tree_edge[v].add(i) #合流点の検出 deq = deque([0]) parent = [-1 for i in range(N)] depth = [0 for i in range(N)] parent[0] = 0 topo = [] while deq: v = deq.popleft() topo.append(v) for nv,cost in edge[v]: if parent[nv]==-1: parent[nv] = v depth[nv] = depth[v] + cost deq.append(nv) topo = topo[::-1] node = [set([]) for v in range(N)] size = [1 for i in range(N)] for i in range(Q): for v in color[i]: node[v].add(i) for v in topo: for nv,cost in edge[v]: if nv!=parent[v]: size[v] += size[nv] if len(node[v]) < len(node[nv]): node[nv],node[v] = node[v],node[nv] for nv,cost in edge[v]: if nv!=parent[v]: for color in node[nv]: if color in node[v]: color_tree_edge[v].add(color) else: node[v].add(color) node[nv] = None res = [0 for i in range(Q)] #辺の張り直し vertex = [] def euler_tour(v,pv): vertex.append(v) for nv,cost in edge[v]: if nv!=pv: euler_tour(nv,v) vertex.append(v) euler_tour(0,0) color_parent = [-1 for i in range(N+1)] visit = [None for i in range(N+1)] for v in vertex: if visit[v] is None: visit[v] = {} for nc in color_tree_edge[v]: if color_parent[nc]!=-1: res[nc] += depth[v] - depth[color_parent[nc]] visit[v][nc] = color_parent[nc] color_parent[nc] = v else: for nc in visit[v]: color_parent[nc] = visit[v][nc] for i in range(Q): print(res[i])