結果

問題 No.317 辺の追加
ユーザー mkawa2mkawa2
提出日時 2023-03-16 11:57:01
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,403 bytes
コンパイル時間 156 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 92,332 KB
最終ジャッジ日時 2023-10-18 12:57:53
合計ジャッジ時間 19,689 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,440 KB
testcase_01 AC 42 ms
55,440 KB
testcase_02 AC 449 ms
85,164 KB
testcase_03 AC 337 ms
82,380 KB
testcase_04 AC 793 ms
88,280 KB
testcase_05 AC 250 ms
79,784 KB
testcase_06 AC 323 ms
82,360 KB
testcase_07 AC 183 ms
78,296 KB
testcase_08 AC 1,444 ms
92,332 KB
testcase_09 AC 528 ms
87,808 KB
testcase_10 AC 431 ms
85,868 KB
testcase_11 AC 321 ms
88,332 KB
testcase_12 AC 394 ms
85,368 KB
testcase_13 AC 468 ms
89,204 KB
testcase_14 AC 452 ms
86,824 KB
testcase_15 AC 450 ms
85,880 KB
testcase_16 AC 693 ms
89,040 KB
testcase_17 AC 442 ms
86,488 KB
testcase_18 AC 440 ms
85,308 KB
testcase_19 AC 414 ms
85,340 KB
testcase_20 AC 772 ms
87,604 KB
testcase_21 AC 234 ms
79,720 KB
testcase_22 AC 197 ms
78,892 KB
testcase_23 AC 254 ms
79,192 KB
testcase_24 AC 275 ms
81,760 KB
testcase_25 AC 271 ms
80,496 KB
testcase_26 AC 280 ms
80,776 KB
testcase_27 AC 268 ms
80,124 KB
testcase_28 AC 366 ms
81,084 KB
testcase_29 AC 121 ms
77,116 KB
testcase_30 TLE -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

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):
    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 or dp[j] == inf: continue
            push(j)
        for i in range(n-si, s-2, -s):
            while 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