Neterukun's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Neterukun1993/Library

:heavy_check_mark: TestCase/yukicoder/yuki0922.HLDecomposition.test.py

Depends on

Code

# verification-helper: PROBLEM https://yukicoder.me/problems/no/922
import sys
input = sys.stdin.buffer.readline

from DataStructure.UnionFind.UnionFind import UnionFind
from Graph.Tree.HLDecomposition import HLDecomposition
from Graph.Tree.rerooting import rerooting


def main():
    n, m, q = map(int, input().split())
    edges = [list(map(int, input().split())) for i in range(m)]
    queries = [list(map(int, input().split())) for i in range(q)]

    tree = [[] for i in range(n)]
    uf = UnionFind(n)
    for i, (u, v) in enumerate(edges):
        u -= 1
        v -= 1
        tree[u].append(v)
        tree[v].append(u)
        uf.merge(u, v)
        edges[i] = (u, v)

    ans = 0
    weights = [0] * n
    hld = HLDecomposition(tree)
    for u, v in queries:
        u -= 1
        v -= 1
        dist = hld.distance(u, v)
        if dist == -1:
            weights[u] += 1
            weights[v] += 1
        else:
            ans += dist

    unit = (0, 0)
    merge = lambda x1, x2: (x1[0] + x2[0], x1[1] + x2[1])
    addnode = lambda x1, v: (x1[0] + weights[v], x1[0] + x1[1])

    res = rerooting(n, edges, unit, merge, addnode)
    for gp in uf.groups():
        min_cost = 10 ** 9
        for i in gp:
            min_cost = min(res[i][1], min_cost)
        ans += min_cost

    print(ans)


if __name__ == '__main__':
    main()
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
    bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/python.py", line 96, in bundle
    raise NotImplementedError
NotImplementedError
Back to top page