結果

問題 No.922 東北きりきざむたん
ユーザー mkawa2mkawa2
提出日時 2022-04-19 16:37:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 595 ms / 2,000 ms
コード長 4,086 bytes
コンパイル時間 1,493 ms
コンパイル使用メモリ 81,852 KB
実行使用メモリ 146,364 KB
最終ジャッジ日時 2024-06-10 13:46:11
合計ジャッジ時間 11,620 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,892 KB
testcase_01 AC 37 ms
53,416 KB
testcase_02 AC 36 ms
53,676 KB
testcase_03 AC 34 ms
53,484 KB
testcase_04 AC 42 ms
62,044 KB
testcase_05 AC 36 ms
54,804 KB
testcase_06 AC 49 ms
65,616 KB
testcase_07 AC 42 ms
64,112 KB
testcase_08 AC 51 ms
68,060 KB
testcase_09 AC 288 ms
98,600 KB
testcase_10 AC 280 ms
86,192 KB
testcase_11 AC 291 ms
94,976 KB
testcase_12 AC 209 ms
93,392 KB
testcase_13 AC 158 ms
81,524 KB
testcase_14 AC 366 ms
108,780 KB
testcase_15 AC 118 ms
91,800 KB
testcase_16 AC 595 ms
122,856 KB
testcase_17 AC 591 ms
120,968 KB
testcase_18 AC 531 ms
119,916 KB
testcase_19 AC 556 ms
121,156 KB
testcase_20 AC 589 ms
120,828 KB
testcase_21 AC 556 ms
127,696 KB
testcase_22 AC 525 ms
126,696 KB
testcase_23 AC 548 ms
128,372 KB
testcase_24 AC 541 ms
131,260 KB
testcase_25 AC 426 ms
127,024 KB
testcase_26 AC 426 ms
127,448 KB
testcase_27 AC 417 ms
126,792 KB
testcase_28 AC 187 ms
94,320 KB
testcase_29 AC 438 ms
146,364 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()
to = [[] for _ in range(n)]
dp = [[0, 0] for u in range(n)]
depth = [0]*n
uf = UnionFind(n)

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

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

# 設定
par = [-1]*n
# pDB(dp)

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

# 渡す前に加える演算
def pre_op(a):
    return [a[0], a[0]+a[1]]

# opの逆算
def inv_op(a, b):
    return [a[0]-b[0], a[1]-b[1]]

# dfs順を作る
def solve(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
            depth[v] = depth[u]+1
            stack.append(v)
        to[u] = vv

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

    # 上から更新
    for u in uu:
        p = par[u]
        if p == -1: continue
        dp[u] = op(dp[u], pre_op(inv_op(dp[p], pre_op(dp[u]))))

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

ans = 0
for u in range(n):
    if par[u] != -1: continue
    ans += solve(u)

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