結果

問題 No.317 辺の追加
ユーザー mkawa2mkawa2
提出日時 2023-03-16 12:02:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,442 ms / 2,000 ms
コード長 2,409 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 82,592 KB
実行使用メモリ 88,308 KB
最終ジャッジ日時 2024-09-18 09:18:16
合計ジャッジ時間 20,428 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
54,400 KB
testcase_01 AC 38 ms
54,272 KB
testcase_02 AC 237 ms
83,088 KB
testcase_03 AC 243 ms
81,560 KB
testcase_04 AC 528 ms
85,036 KB
testcase_05 AC 198 ms
80,256 KB
testcase_06 AC 232 ms
81,628 KB
testcase_07 AC 151 ms
78,160 KB
testcase_08 AC 1,133 ms
87,556 KB
testcase_09 AC 281 ms
84,548 KB
testcase_10 AC 285 ms
83,052 KB
testcase_11 AC 235 ms
88,244 KB
testcase_12 AC 263 ms
83,324 KB
testcase_13 AC 359 ms
88,308 KB
testcase_14 AC 254 ms
83,624 KB
testcase_15 AC 277 ms
83,648 KB
testcase_16 AC 463 ms
85,640 KB
testcase_17 AC 281 ms
83,756 KB
testcase_18 AC 266 ms
83,328 KB
testcase_19 AC 262 ms
83,552 KB
testcase_20 AC 530 ms
86,656 KB
testcase_21 AC 122 ms
79,916 KB
testcase_22 AC 117 ms
78,592 KB
testcase_23 AC 151 ms
79,356 KB
testcase_24 AC 144 ms
82,120 KB
testcase_25 AC 144 ms
80,868 KB
testcase_26 AC 164 ms
80,780 KB
testcase_27 AC 141 ms
79,968 KB
testcase_28 AC 173 ms
79,584 KB
testcase_29 AC 89 ms
77,568 KB
testcase_30 AC 1,377 ms
83,620 KB
testcase_31 AC 981 ms
83,624 KB
testcase_32 AC 919 ms
82,688 KB
testcase_33 AC 907 ms
82,840 KB
testcase_34 AC 929 ms
83,080 KB
testcase_35 AC 940 ms
82,940 KB
testcase_36 AC 974 ms
83,364 KB
testcase_37 AC 940 ms
82,948 KB
testcase_38 AC 1,442 ms
84,300 KB
testcase_39 AC 948 ms
82,692 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

from collections import Counter, deque

class UnionFind:
    def __init__(self, n):
        self.state = [-1]*n
        self.size_table = [1]*n
        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)]

n, m = LI()
uf = UnionFind(n)
for _ in range(m):
    u, v = LI1()
    uf.merge(u, v)

seen = set()
cnt = Counter()
for u in range(n):
    r = uf.root(u)
    if r in seen: continue
    seen.add(r)
    cnt[uf.size(r)] += 1
# pDB(cnt)

def push(i):
    if dp[i] == inf: return
    val = dp[i]*s-i
    while mns and mns[-1][-1] >= val: mns.pop()
    mns.append((i, val))

dp = [-1]+[inf]*n
for s, c in cnt.items():
    for si in range(s):
        mns = deque()
        for k in range(c-1):
            j = n-si-s*(k+1)
            if j < 0: break
            push(j)
        for i in range(n-si, s-1, -s):
            if mns and mns[0][0] > i-s: mns.popleft()
            if i-s*c >= 0: push(i-s*c)
            if mns: dp[i] = min(dp[i], (mns[0][1]+i)//s)

for i in range(n):
    if dp[i] == inf: dp[i] = -1
print(*dp[1:], sep="\n")
0