結果
問題 | No.1103 Directed Length Sum |
ユーザー | Navier_Boltzmann |
提出日時 | 2022-07-17 19:49:06 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 4,259 bytes |
コンパイル時間 | 165 ms |
コンパイル使用メモリ | 82,268 KB |
実行使用メモリ | 848,668 KB |
最終ジャッジ日時 | 2024-06-29 20:51:11 |
合計ジャッジ時間 | 6,887 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
56,992 KB |
testcase_01 | AC | 45 ms
57,128 KB |
testcase_02 | MLE | - |
testcase_03 | -- | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
ソースコード
import pypyjit pypyjit.set_param('max_unroll_recursion=-1') from collections import * from itertools import * from functools import * from heapq import * import sys input = sys.stdin.readline sys.setrecursionlimit(10**6) mod = 10**9 + 7 N = int(input()) import sys sys.setrecursionlimit(10**6) class LCA(): def __init__(self,e,root=0): self.N = len(e) self.e = e self.root = root self.IN = [-1]*N self.OUT = [-1]*N self.DIST = [-1]*N self.time = 0 self.DIST[root] = 0 self.parents = [-1]*N self.dfs(root) self.depth = [(self.DIST[i],i) for i in range(N)] self.init_val = [-1]*(2*N) for i in range(N): x = self.IN[i] y = self.OUT[i] self.init_val[x] = self.depth[i] self.init_val[y] = self.depth[self.parents[i]] self.T = self.SegTree(self.init_val) def dfs(self,x): self.IN[x] = self.time self.time += 1 for ix in e[x]: if self.DIST[ix] != -1: continue self.parents[ix] = x self.DIST[ix] = self.DIST[x] + 1 self.dfs(ix) self.OUT[x] = self.time self.time += 1 def lca(self,l,r): L = self.IN[l] R = self.IN[r] if R<L: L,R = R,L R += 1 return self.T.query(L,R)[1] def distance_between(self,l,r): p = self.lca(l,r) return self.DIST[l]+ self.DIST[r] - 2*self.DIST[p] def distance_from_root(self,x): return self.DIST[x] class SegTree: """ Segment Tree """ def __init__(self, init_val, segfunc = None, ide_ele= None): """ 初期化 init_val: 配列の初期値 """ if segfunc is None: def segfunc(x,y): return min(x,y) ide_ele = (float('inf'),-1) n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res edge = [] e = [[] for _ in range(N)] flg = [True]*N for _ in range(N-1): a,b = map(int,input().split()) a -= 1 b -= 1 e[a].append(b) e[b].append(a) flg[b] = False edge.append((a,b)) for i in range(N): if flg[i]: root = i break parents = [-1]*N sub = [-1]*N def dfs(x): tmp = 1 if len(e[x]) == 1 and x != root: sub[x] = 1 return 1 for ix in e[x]: if ix == parents[x]: continue parents[ix] = x if sub[ix] != -1: tmp += sub[ix] else: tmp += dfs(ix) sub[x] = tmp return tmp dfs(root) L = LCA(e,root) ans = 0 for a,b in edge: ans += (L.DIST[a]+1)*(sub[b]) ans %= mod print(ans)