結果
問題 | No.439 チワワのなる木 |
ユーザー | maspy |
提出日時 | 2020-03-07 03:13:46 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 533 ms / 5,000 ms |
コード長 | 1,730 bytes |
コンパイル時間 | 136 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 48,996 KB |
最終ジャッジ日時 | 2024-10-14 11:07:21 |
合計ジャッジ時間 | 5,784 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 31 ms
10,880 KB |
testcase_01 | AC | 33 ms
10,752 KB |
testcase_02 | AC | 31 ms
10,880 KB |
testcase_03 | AC | 29 ms
10,880 KB |
testcase_04 | AC | 30 ms
10,880 KB |
testcase_05 | AC | 28 ms
10,752 KB |
testcase_06 | AC | 28 ms
10,752 KB |
testcase_07 | AC | 28 ms
10,880 KB |
testcase_08 | AC | 28 ms
10,880 KB |
testcase_09 | AC | 28 ms
10,880 KB |
testcase_10 | AC | 29 ms
10,752 KB |
testcase_11 | AC | 27 ms
11,008 KB |
testcase_12 | AC | 28 ms
11,008 KB |
testcase_13 | AC | 29 ms
10,880 KB |
testcase_14 | AC | 31 ms
10,880 KB |
testcase_15 | AC | 33 ms
11,136 KB |
testcase_16 | AC | 36 ms
11,392 KB |
testcase_17 | AC | 34 ms
11,264 KB |
testcase_18 | AC | 373 ms
33,540 KB |
testcase_19 | AC | 344 ms
32,648 KB |
testcase_20 | AC | 501 ms
40,532 KB |
testcase_21 | AC | 146 ms
20,064 KB |
testcase_22 | AC | 130 ms
18,844 KB |
testcase_23 | AC | 533 ms
42,896 KB |
testcase_24 | AC | 476 ms
38,996 KB |
testcase_25 | AC | 495 ms
42,452 KB |
testcase_26 | AC | 480 ms
41,004 KB |
testcase_27 | AC | 416 ms
48,996 KB |
ソースコード
#!/usr/bin/env python3 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # %% N = int(readline()) S = ' ' + readline().decode().rstrip() m = map(int, read().split()) AB = zip(m, m) # %% graph = [[] for _ in range(N + 1)] for a, b in AB: graph[a].append(b) graph[b].append(a) # %% root = 1 parent = [0] * (N + 1) order = [] stack = [root] while stack: x = stack.pop() order.append(x) for y in graph[x]: if y == parent[x]: continue parent[y] = x stack.append(y) # %% dp1_w = [0] * (N + 1) dp1_ww = [0] * (N + 1) for v in order[::-1]: p = parent[v] dp1_w[p] += dp1_w[v] dp1_ww[p] += dp1_ww[v] if S[v] == 'w': dp1_w[p] += 1 dp1_ww[p] += dp1_w[v] dp1_w, dp1_ww # %% dp2_w = [0] * (N + 1) dp2_ww = [0] * (N + 1) for v in order[1:]: p = parent[v] if S[p] == 'w' and S[v] == 'w': dp2_w[v] = dp2_w[p] + dp1_w[p] - (1 + dp1_w[v]) + 1 dp2_ww[v] = (dp2_ww[p] + dp2_w[p]) + (dp1_ww[p] + dp1_w[p]) - (dp1_ww[v] + 2 * dp1_w[v] + 1) elif S[p] == 'w' and S[v] == 'c': dp2_w[v] = dp2_w[p] + dp1_w[p] - (dp1_w[v]) + 1 dp2_ww[v] = (dp2_ww[p] + dp2_w[p]) + (dp1_ww[p] + dp1_w[p]) - dp1_w[v] - dp1_ww[v] elif S[p] == 'c' and S[v] == 'w': dp2_w[v] = dp2_w[p] + dp1_w[p] - (1 + dp1_w[v]) dp2_ww[v] = dp2_ww[p] + dp1_ww[p] - (dp1_ww[v] + dp1_w[v]) elif S[p] == 'c' and S[v] == 'c': dp2_w[v] = dp2_w[p] + dp1_w[p] - dp1_w[v] dp2_ww[v] = dp2_ww[p] + dp1_ww[p] - dp1_ww[v] dp2_w, dp2_ww # %% ww = [x + y for x, y in zip(dp1_ww, dp2_ww)] answer = sum(x * (ch == 'c') for x, ch in zip(ww, S)) print(answer)