結果
問題 | No.1843 Tree ANDistance |
ユーザー | mkawa2 |
提出日時 | 2023-02-16 10:48:11 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,056 ms / 2,000 ms |
コード長 | 1,492 bytes |
コンパイル時間 | 251 ms |
コンパイル使用メモリ | 82,256 KB |
実行使用メモリ | 143,020 KB |
最終ジャッジ日時 | 2024-07-18 12:39:07 |
合計ジャッジ時間 | 20,142 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,056 ms
140,660 KB |
testcase_01 | AC | 1,020 ms
140,600 KB |
testcase_02 | AC | 963 ms
140,628 KB |
testcase_03 | AC | 992 ms
140,504 KB |
testcase_04 | AC | 953 ms
140,456 KB |
testcase_05 | AC | 686 ms
139,492 KB |
testcase_06 | AC | 824 ms
143,020 KB |
testcase_07 | AC | 927 ms
137,344 KB |
testcase_08 | AC | 978 ms
140,756 KB |
testcase_09 | AC | 944 ms
137,276 KB |
testcase_10 | AC | 908 ms
137,796 KB |
testcase_11 | AC | 952 ms
139,560 KB |
testcase_12 | AC | 625 ms
136,176 KB |
testcase_13 | AC | 787 ms
133,468 KB |
testcase_14 | AC | 97 ms
77,568 KB |
testcase_15 | AC | 102 ms
78,080 KB |
testcase_16 | AC | 79 ms
75,164 KB |
testcase_17 | AC | 83 ms
77,056 KB |
testcase_18 | AC | 53 ms
65,024 KB |
testcase_19 | AC | 90 ms
78,080 KB |
testcase_20 | AC | 84 ms
77,184 KB |
testcase_21 | AC | 36 ms
52,096 KB |
testcase_22 | AC | 34 ms
52,096 KB |
testcase_23 | AC | 34 ms
52,480 KB |
testcase_24 | AC | 35 ms
52,736 KB |
testcase_25 | AC | 34 ms
52,096 KB |
testcase_26 | AC | 39 ms
52,480 KB |
testcase_27 | AC | 38 ms
52,096 KB |
testcase_28 | AC | 707 ms
122,244 KB |
testcase_29 | AC | 447 ms
110,680 KB |
testcase_30 | AC | 451 ms
110,624 KB |
testcase_31 | AC | 743 ms
128,296 KB |
testcase_32 | AC | 727 ms
122,900 KB |
testcase_33 | AC | 205 ms
96,496 KB |
testcase_34 | AC | 481 ms
118,332 KB |
testcase_35 | AC | 35 ms
52,096 KB |
testcase_36 | AC | 34 ms
52,352 KB |
testcase_37 | AC | 34 ms
52,608 KB |
ソースコード
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)] cc = {} for _ in range(n-1): u, v, c = LI1() c += 1 to[u].append(v) to[v].append(u) cc[u, v] = cc[v, u] = c parent, depth, vis = [-1]*n, [0]*n, [0]*n def dfs(root=0): uu, stack = [], [root] vis[root] = 1 while stack: u = stack.pop() uu.append(u) for v in to[u]: if vis[v]: continue vis[v] = 1 parent[v] = u stack.append(v) return uu uu = dfs() ans = 0 for i in range(30)[::-1]: cnt = [1]*n s = 0 for u in uu[:0:-1]: p = parent[u] if cc[p, u] >> i & 1: s += cnt[p]*cnt[u]%md s %= md cnt[p] += cnt[u] ans += pow(2, i, md)*s%md ans %= md print(ans)