結果
問題 | No.439 チワワのなる木 |
ユーザー | maspy |
提出日時 | 2020-03-07 03:08:28 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,706 bytes |
コンパイル時間 | 109 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 50,412 KB |
最終ジャッジ日時 | 2024-10-14 11:07:11 |
合計ジャッジ時間 | 7,120 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 32 ms
10,880 KB |
testcase_01 | AC | 30 ms
11,008 KB |
testcase_02 | AC | 32 ms
11,136 KB |
testcase_03 | WA | - |
testcase_04 | AC | 32 ms
10,880 KB |
testcase_05 | AC | 31 ms
10,880 KB |
testcase_06 | AC | 32 ms
10,880 KB |
testcase_07 | WA | - |
testcase_08 | AC | 33 ms
10,880 KB |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | AC | 31 ms
10,880 KB |
testcase_12 | AC | 33 ms
10,880 KB |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 616 ms
39,128 KB |
testcase_25 | AC | 613 ms
42,532 KB |
testcase_26 | AC | 575 ms
41,008 KB |
testcase_27 | WA | - |
ソースコード
#!/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] 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] 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)