結果
問題 | No.1843 Tree ANDistance |
ユーザー | mkawa2 |
提出日時 | 2023-09-26 23:32:36 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 426 ms / 2,000 ms |
コード長 | 1,531 bytes |
コンパイル時間 | 415 ms |
コンパイル使用メモリ | 82,076 KB |
実行使用メモリ | 157,004 KB |
最終ジャッジ日時 | 2024-07-19 17:16:56 |
合計ジャッジ時間 | 11,896 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 420 ms
141,628 KB |
testcase_01 | AC | 411 ms
134,320 KB |
testcase_02 | AC | 408 ms
147,200 KB |
testcase_03 | AC | 426 ms
141,380 KB |
testcase_04 | AC | 411 ms
147,136 KB |
testcase_05 | AC | 361 ms
157,004 KB |
testcase_06 | AC | 390 ms
145,836 KB |
testcase_07 | AC | 397 ms
144,384 KB |
testcase_08 | AC | 417 ms
145,956 KB |
testcase_09 | AC | 405 ms
143,872 KB |
testcase_10 | AC | 400 ms
142,976 KB |
testcase_11 | AC | 426 ms
146,432 KB |
testcase_12 | AC | 344 ms
143,056 KB |
testcase_13 | AC | 365 ms
147,712 KB |
testcase_14 | AC | 98 ms
77,184 KB |
testcase_15 | AC | 95 ms
77,236 KB |
testcase_16 | AC | 74 ms
71,808 KB |
testcase_17 | AC | 80 ms
71,424 KB |
testcase_18 | AC | 61 ms
63,744 KB |
testcase_19 | AC | 88 ms
77,312 KB |
testcase_20 | AC | 82 ms
73,856 KB |
testcase_21 | AC | 41 ms
52,608 KB |
testcase_22 | AC | 41 ms
52,204 KB |
testcase_23 | AC | 41 ms
52,480 KB |
testcase_24 | AC | 41 ms
52,480 KB |
testcase_25 | AC | 42 ms
52,608 KB |
testcase_26 | AC | 41 ms
52,480 KB |
testcase_27 | AC | 42 ms
51,968 KB |
testcase_28 | AC | 326 ms
131,584 KB |
testcase_29 | AC | 238 ms
112,812 KB |
testcase_30 | AC | 211 ms
113,664 KB |
testcase_31 | AC | 319 ms
137,216 KB |
testcase_32 | AC | 298 ms
132,480 KB |
testcase_33 | AC | 122 ms
96,000 KB |
testcase_34 | AC | 252 ms
123,904 KB |
testcase_35 | AC | 40 ms
51,968 KB |
testcase_36 | AC | 40 ms
51,968 KB |
testcase_37 | AC | 38 ms
52,480 KB |
ソースコード
import sys # sys.setrecursionlimit(200005) # sys.set_int_max_str_digits(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-(-1 << 31) inf = -1-(-1 << 63) md = 10**9+7 # md = 998244353 def dfs(root=0): uu, stack = [], [root] while stack: u = stack.pop() uu.append(u) for v, c in to[u]: if v == parent[u]: continue cc[v] = c parent[v] = u depth[v] = depth[u]+1 stack.append(v) return uu n = II() to = [[] for _ in range(n)] for _ in range(n-1): u, v, c = LI1() to[u].append((v, c+1)) to[v].append((u, c+1)) parent, depth = [-1]*n, [0]*n cc = [0]*n uu = dfs() ans = 0 for k in range(30): dp = [1]*n cur = 0 for u in uu[:0:-1]: if cc[u] >> k & 1: dp[parent[u]] += dp[u] else: cur += dp[u]*(dp[u]-1)//2 cur += dp[0]*(dp[0]-1)//2 ans += cur*pow(2, k, md)%md ans %= md print(ans)