結果

問題 No.1817 Reversed Edges
ユーザー zkouzkou
提出日時 2022-01-21 23:26:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 356 ms / 2,000 ms
コード長 4,083 bytes
コンパイル時間 290 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 128,524 KB
最終ジャッジ日時 2024-05-04 15:01:33
合計ジャッジ時間 7,247 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,504 KB
testcase_01 AC 37 ms
53,088 KB
testcase_02 AC 38 ms
53,248 KB
testcase_03 AC 38 ms
53,376 KB
testcase_04 AC 39 ms
53,504 KB
testcase_05 AC 38 ms
53,248 KB
testcase_06 AC 38 ms
53,248 KB
testcase_07 AC 312 ms
107,256 KB
testcase_08 AC 215 ms
92,172 KB
testcase_09 AC 282 ms
106,304 KB
testcase_10 AC 217 ms
93,680 KB
testcase_11 AC 258 ms
99,176 KB
testcase_12 AC 342 ms
112,956 KB
testcase_13 AC 323 ms
112,580 KB
testcase_14 AC 317 ms
112,468 KB
testcase_15 AC 323 ms
112,596 KB
testcase_16 AC 322 ms
112,392 KB
testcase_17 AC 310 ms
111,820 KB
testcase_18 AC 317 ms
112,336 KB
testcase_19 AC 328 ms
112,592 KB
testcase_20 AC 322 ms
112,220 KB
testcase_21 AC 356 ms
112,704 KB
testcase_22 AC 221 ms
126,140 KB
testcase_23 AC 238 ms
128,524 KB
testcase_24 AC 227 ms
121,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class heavy_light_decomposition:
    "The heavy edge oriented from the vertex `v` is the edge between `v` and `adj[vertex][0]`"

    def __init__(self, N, adj, root=0):
        self.N = N
        self.adj = [row.copy() for row in adj]
        self.root = root

        self.sz = [1] * N
        self.in_ = [0] * N
        self.out = [0] * N
        self.head = [0] * N
        self.rev = [0] * N
        self.par = [-1] * N

        self._dfs_sz()
        self._dfs_hld()

    def _dfs_sz(self):
        stack = []
        stack.append(~self.root)
        stack.append(self.root)
        while stack:
            v = stack.pop()
            if v >= 0:
                for nv in self.adj[v]:
                    if nv == self.par[v]:
                        continue
                    self.par[nv] = v
                    stack.append(~nv)
                    stack.append(nv)
            else:
                v = ~v
                if v and self.adj[v][0] == self.par[v]:
                    self.adj[v][0], self.adj[v][-1] = self.adj[v][-1], self.adj[v][0]
                for i, nv in enumerate(self.adj[v]):
                    if nv == self.par[v]:
                        continue
                    if self.sz[self.adj[v][0]] < self.sz[nv]:
                        self.adj[v][0], self.adj[v][i] = self.adj[v][i], self.adj[v][0]
                    self.sz[v] += self.sz[nv]

    def _dfs_hld(self):
        t = 0
        self.head[self.root] = self.root
        stack = []
        stack.append(~self.root)
        stack.append(self.root)
        while stack:
            v = stack.pop()
            if v >= 0:
                self.in_[v] = t
                self.rev[t] = v
                t += 1
                for nv in reversed(self.adj[v]):
                    if nv == self.par[v]:
                        continue
                    self.head[nv] = self.head[v] if self.adj[v][0] == nv else nv
                    stack.append(~nv)
                    stack.append(nv)
            else:
                v = ~v
                self.out[v] = t

    def level_ancestor(self, v, level):
        while True:
            u = self.head[v]
            if self.in_[v] - level >= self.in_[u]:
                return self.rev[self.in_[v] - level]
            level -= self.in_[v] - self.in_[u] + 1
            v = self.par[u]

    def lowest_common_ancestor(self, u, v):
        while True:
            if self.in_[u] > self.in_[v]:
                u, v = v, u
            if self.head[u] == self.head[v]:
                return u
            v = self.par[self.head[v]]

    def query(self, u, v, e, q, f, edge=False):
        l = e()
        r = e()
        while True:
            if self.in_[u] > self.in_[v]:
                u, v = v, u
                l, r = r, l
            if self.head[u] == self.head[v]:
                break
            l = f(q(self.in_[self.head[v]], self.in_[v] + 1), l)
            v = self.par[self.head[v]]
        return f(f(q(self.in_[u] + edge, self.in_[v] + 1), l), r)

    def add(self, u, v, q, edge=False):
        while True:
            if self.in_[u] > self.in_[v]:
                u, v = v, u
            if self.head[u] == self.head[v]:
                break
            q(self.in_[self.head[v]], self.in_[v] + 1)
            v = self.par[self.head[v]]
        q(self.in_[u] + edge, self.in_[v] + 1)

    def subtree(self, v, edge=False):
        return (self.in_[v] + edge, self.out[v])


N = int(input())
edges = []
adj = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    u -= 1; v -= 1
    edges.append((u, v))
    adj[u].append(v)
    adj[v].append(u)

hld = heavy_light_decomposition(N, adj)
imos = [0] * (N + 1)
for u, v in edges:
    d = 1 if u < v else -1
    if hld.in_[u] > hld.in_[v]:
        u, v = v, u
        d *= -1
    l, r = hld.subtree(v)
    imos[l] += d
    imos[r] -= d
    if d == -1:
        imos[0] += 1
        imos[-1] -= 1

for i in range(N):
    imos[i + 1] += imos[i]

answer = [0] * N
for t in range(N):
    answer[hld.rev[t]] = imos[t]

print(*answer, sep='\n')
0