結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー mkawa2mkawa2
提出日時 2023-02-03 22:16:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 317 ms / 2,000 ms
コード長 1,624 bytes
コンパイル時間 363 ms
コンパイル使用メモリ 87,128 KB
実行使用メモリ 164,484 KB
最終ジャッジ日時 2023-09-15 18:16:11
合計ジャッジ時間 11,436 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,580 KB
testcase_01 AC 71 ms
71,380 KB
testcase_02 AC 70 ms
71,444 KB
testcase_03 AC 75 ms
71,456 KB
testcase_04 AC 71 ms
71,372 KB
testcase_05 AC 76 ms
71,436 KB
testcase_06 AC 75 ms
71,028 KB
testcase_07 AC 72 ms
71,472 KB
testcase_08 AC 69 ms
71,412 KB
testcase_09 AC 71 ms
71,416 KB
testcase_10 AC 71 ms
71,316 KB
testcase_11 AC 279 ms
149,636 KB
testcase_12 AC 281 ms
149,252 KB
testcase_13 AC 292 ms
149,508 KB
testcase_14 AC 272 ms
156,256 KB
testcase_15 AC 279 ms
164,484 KB
testcase_16 AC 235 ms
114,572 KB
testcase_17 AC 223 ms
114,524 KB
testcase_18 AC 73 ms
71,376 KB
testcase_19 AC 289 ms
147,784 KB
testcase_20 AC 290 ms
148,972 KB
testcase_21 AC 317 ms
148,880 KB
testcase_22 AC 279 ms
133,572 KB
testcase_23 AC 292 ms
145,296 KB
testcase_24 AC 307 ms
147,576 KB
testcase_25 AC 316 ms
147,044 KB
testcase_26 AC 300 ms
153,420 KB
testcase_27 AC 278 ms
153,208 KB
testcase_28 AC 264 ms
133,932 KB
testcase_29 AC 284 ms
139,816 KB
testcase_30 AC 297 ms
149,260 KB
testcase_31 AC 279 ms
151,976 KB
testcase_32 AC 287 ms
151,784 KB
testcase_33 AC 298 ms
151,928 KB
testcase_34 AC 269 ms
148,500 KB
testcase_35 AC 282 ms
149,640 KB
testcase_36 AC 289 ms
148,448 KB
testcase_37 AC 304 ms
145,016 KB
testcase_38 AC 309 ms
148,800 KB
testcase_39 AC 299 ms
147,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
# md = 10**9+7
md = 998244353

n = II()
to = [[] for _ in range(n)]
for _ in range(n-1):
    u, v = LI1()
    to[u].append(v)
    to[v].append(u)
cc = LI()

parent, depth, vis = [-1]*n, [0]*n, [0]*n

def dfs(root):
    uu = []
    vis[root] = 1
    stack = [root]
    while stack:
        iu = stack.pop()
        i, u = divmod(iu, n)
        if i == 0:
            uu.append(u)
        # care to's format
        while i < len(to[u]) and vis[to[u][i]]: i += 1
        if i < len(to[u]):
            stack.append((i+1)*n+u)
            # care to's format
            v = to[u][i]
            vis[v] = 1
            stack.append(v)
            parent[v] = u
            depth[v] = depth[u]+1

    return uu

uu = dfs(0)
cc = [c ^ 1 for c in cc]
dp = [0]*n

for u in uu[::-1]:
    if u==0:break
    p = parent[u]
    dp[p] += dp[u]
    if cc[u]:
        dp[p] += 1
        cc[p] ^= 1

if cc[0]:
    print(-1)
else:
    print(dp[0])
0