結果

問題 No.922 東北きりきざむたん
ユーザー mkawa2mkawa2
提出日時 2022-04-22 15:46:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 785 ms / 2,000 ms
コード長 4,379 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 82,356 KB
実行使用メモリ 161,012 KB
最終ジャッジ日時 2024-06-23 20:34:19
合計ジャッジ時間 13,723 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,504 KB
testcase_01 AC 39 ms
53,632 KB
testcase_02 AC 38 ms
52,992 KB
testcase_03 AC 38 ms
53,504 KB
testcase_04 AC 49 ms
62,592 KB
testcase_05 AC 42 ms
55,680 KB
testcase_06 AC 57 ms
67,712 KB
testcase_07 AC 50 ms
63,744 KB
testcase_08 AC 62 ms
69,760 KB
testcase_09 AC 380 ms
107,820 KB
testcase_10 AC 339 ms
89,168 KB
testcase_11 AC 367 ms
101,724 KB
testcase_12 AC 253 ms
106,112 KB
testcase_13 AC 187 ms
84,860 KB
testcase_14 AC 481 ms
123,328 KB
testcase_15 AC 179 ms
104,832 KB
testcase_16 AC 750 ms
139,692 KB
testcase_17 AC 778 ms
139,100 KB
testcase_18 AC 724 ms
138,176 KB
testcase_19 AC 742 ms
139,272 KB
testcase_20 AC 728 ms
138,116 KB
testcase_21 AC 700 ms
146,252 KB
testcase_22 AC 711 ms
145,720 KB
testcase_23 AC 785 ms
148,232 KB
testcase_24 AC 759 ms
149,180 KB
testcase_25 AC 577 ms
143,812 KB
testcase_26 AC 589 ms
144,032 KB
testcase_27 AC 573 ms
143,836 KB
testcase_28 AC 265 ms
110,412 KB
testcase_29 AC 663 ms
161,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class UnionFind:
    def __init__(self, n):
        self.state = [-1]*n
        self.size_table = [1]*n
        # cntはグループ数
        self.cnt = n

    def root(self, u):
        stack = []
        while self.state[u] >= 0:
            stack.append(u)
            u = self.state[u]
        for v in stack: self.state[v] = u
        return u

    def same(self, u, v):
        return self.root(u) == self.root(v)

    def merge(self, u, v):
        u = self.root(u)
        v = self.root(v)
        if u == v: return False
        du = -self.state[u]
        dv = -self.state[v]
        if du < dv: u, v = v, u
        if du == dv: self.state[u] -= 1
        self.state[v] = u
        self.cnt -= 1
        self.size_table[u] += self.size_table[v]
        return True

    # グループの要素数
    def size(self, u):
        return self.size_table[self.root(u)]

class LCA:
    # 頂点は0~n-1
    def __init__(self, parents, depth):
        self.n = len(parents)
        self.parents = parents
        self.depth = depth
        self.max_level = max(self.depth).bit_length()
        self.ancestor = [self.parents]+[[-1]*self.n for _ in range(self.max_level)]
        row0 = self.ancestor[0]
        for lv in range(self.max_level):
            row1 = self.ancestor[lv+1]
            for u in range(self.n):
                row1[u] = row0[row0[u]]
            row0 = row1

    # 最小共通祖先
    def anc(self, u, v):
        diff = self.depth[u]-self.depth[v]
        if diff < 0: u, v = v, u
        diff = abs(diff)
        lv = 0
        while diff:
            if diff & 1: u = self.ancestor[lv][u]
            lv, diff = lv+1, diff >> 1
        if u == v: return u
        for lv in range(self.depth[u].bit_length()-1, -1, -1):
            anclv = self.ancestor[lv]
            if anclv[u] != anclv[v]: u, v = anclv[u], anclv[v]
        return self.parents[u]

n, m, q = LI()
e = lambda: [0, 0]  # 単位元
par = [-1]*n
depth = [0]*n
uf = UnionFind(n)
to = [[] for _ in range(n)]

for _ in range(m):
    u, v = LI1()
    to[u].append(v)
    to[v].append(u)
    uf.merge(u, v)

dp = [e() for _ in range(n)]
rdp = [e() for _ in range(n)]
ab = []
for _ in range(q):
    a, b = LI1()
    if uf.same(a, b):
        ab.append((a, b))
    else:
        dp[a][0] += 1
        dp[b][0] += 1

def op(a, b):
    return [a[0]+b[0], a[1]+b[1]]

def trans_up(u):
    dp[u][1] += dp[u][0]
    return

def trans_down(u):
    rdp[u][1] += rdp[u][0]
    return

# dfs順を作る
def reroot(root):
    uu = []
    stack = [root]
    while stack:
        u = stack.pop()
        uu.append(u)
        vv = []
        for v in to[u]:
            if v == par[u]: continue
            vv.append(v)
            par[v] = u
            stack.append(v)
            rdp[v] = dp[u]
            depth[v] = depth[u]+1
        to[u] = vv
    # pDB(uu)
    # pDB(dp)
    # pDB(rdp)

    # 下から更新
    for u in uu[::-1]:
        p = par[u]
        if p != -1:
            trans_up(u)
            dp[p] = op(dp[p], dp[u])
    # pDB(rdp)

    # 上から更新
    for u in uu:
        cs = e()
        # ↓変数注意
        for v in to[u]:
            rdp[v] = op(rdp[v], cs)
            cs = op(cs, dp[v])
        cs = op(e(), rdp[u])
        for v in to[u][::-1]:
            rdp[v] = op(rdp[v], cs)
            trans_down(v)
            cs = op(cs, dp[v])
        dp[u] = cs

    return min(dp[u][1] for u in uu)

ans = 0
for u in range(n):
    if par[u] != -1: continue
    ans += reroot(u)
    # pDB(dp, ans)

lca = LCA(par, depth)
for u, v in ab:
    a = lca.anc(u, v)
    ans += depth[u]+depth[v]-depth[a]*2

print(ans)
# pDB(dp)
# pDB(par)
0