結果
問題 | No.901 K-ary εxtrεεmε |
ユーザー | chineristAC |
提出日時 | 2020-09-27 22:40:45 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,059 bytes |
コンパイル時間 | 220 ms |
コンパイル使用メモリ | 82,364 KB |
実行使用メモリ | 182,600 KB |
最終ジャッジ日時 | 2024-06-30 14:04:09 |
合計ジャッジ時間 | 22,109 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | AC | 41 ms
55,048 KB |
testcase_02 | AC | 59 ms
69,612 KB |
testcase_03 | AC | 58 ms
68,320 KB |
testcase_04 | AC | 58 ms
68,772 KB |
testcase_05 | AC | 59 ms
69,524 KB |
testcase_06 | AC | 59 ms
68,612 KB |
testcase_07 | AC | 804 ms
174,464 KB |
testcase_08 | AC | 782 ms
175,348 KB |
testcase_09 | AC | 781 ms
167,348 KB |
testcase_10 | AC | 828 ms
173,716 KB |
testcase_11 | AC | 807 ms
174,996 KB |
testcase_12 | AC | 774 ms
163,576 KB |
testcase_13 | AC | 804 ms
166,360 KB |
testcase_14 | AC | 782 ms
161,668 KB |
testcase_15 | AC | 784 ms
162,540 KB |
testcase_16 | AC | 766 ms
161,952 KB |
testcase_17 | AC | 854 ms
176,684 KB |
testcase_18 | AC | 845 ms
179,644 KB |
testcase_19 | AC | 857 ms
179,104 KB |
testcase_20 | AC | 840 ms
175,896 KB |
testcase_21 | AC | 837 ms
178,412 KB |
testcase_22 | AC | 732 ms
157,708 KB |
testcase_23 | AC | 758 ms
161,548 KB |
testcase_24 | AC | 735 ms
160,576 KB |
testcase_25 | AC | 726 ms
159,156 KB |
testcase_26 | AC | 775 ms
165,164 KB |
testcase_27 | AC | 844 ms
182,116 KB |
testcase_28 | AC | 851 ms
181,628 KB |
testcase_29 | AC | 875 ms
182,600 KB |
ソースコード
import sys from collections import deque input = sys.stdin.readline 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])