結果

問題 No.1669 パズル作成
ユーザー penguinmanpenguinman
提出日時 2021-04-27 23:18:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 845 ms / 2,000 ms
コード長 2,894 bytes
コンパイル時間 227 ms
コンパイル使用メモリ 82,328 KB
実行使用メモリ 79,616 KB
最終ジャッジ日時 2024-05-09 00:33:04
合計ジャッジ時間 15,013 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
52,864 KB
testcase_01 AC 43 ms
52,864 KB
testcase_02 AC 40 ms
52,864 KB
testcase_03 AC 66 ms
71,424 KB
testcase_04 AC 62 ms
68,224 KB
testcase_05 AC 61 ms
71,040 KB
testcase_06 AC 125 ms
77,696 KB
testcase_07 AC 783 ms
76,888 KB
testcase_08 AC 167 ms
79,040 KB
testcase_09 AC 845 ms
76,820 KB
testcase_10 AC 608 ms
77,608 KB
testcase_11 AC 667 ms
77,312 KB
testcase_12 AC 170 ms
79,616 KB
testcase_13 AC 164 ms
79,360 KB
testcase_14 AC 165 ms
79,308 KB
testcase_15 AC 662 ms
77,312 KB
testcase_16 AC 803 ms
76,800 KB
testcase_17 AC 310 ms
77,840 KB
testcase_18 AC 801 ms
77,056 KB
testcase_19 AC 822 ms
76,800 KB
testcase_20 AC 745 ms
77,540 KB
testcase_21 AC 704 ms
77,440 KB
testcase_22 AC 631 ms
77,952 KB
testcase_23 AC 569 ms
77,560 KB
testcase_24 AC 485 ms
78,428 KB
testcase_25 AC 424 ms
78,152 KB
testcase_26 AC 280 ms
78,124 KB
testcase_27 AC 808 ms
77,580 KB
testcase_28 AC 699 ms
77,312 KB
testcase_29 AC 331 ms
78,284 KB
testcase_30 AC 140 ms
78,336 KB
testcase_31 AC 142 ms
78,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# Reference: https://github.com/shakayami/ACL-for-python
class dsu():
    n=1
    parent_or_size=[-1 for i in range(n)]
    def __init__(self,N):
        self.n=N
        self.parent_or_size=[-1 for i in range(N)]
    def merge(self,a,b):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n)
        x=self.leader(a)
        y=self.leader(b)
        if x==y:
            return x
        if (-self.parent_or_size[x]<-self.parent_or_size[y]):
            x,y=y,x
        self.parent_or_size[x]+=self.parent_or_size[y]
        self.parent_or_size[y]=x
        return x
    def same(self,a,b):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n)
        return self.leader(a)==self.leader(b)
    def leader(self,a):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        if (self.parent_or_size[a]<0):
            return a
        self.parent_or_size[a]=self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]
    def size(self,a):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        return -self.parent_or_size[self.leader(a)]
    def groups(self):
        leader_buf=[0 for i in range(self.n)]
        group_size=[0 for i in range(self.n)]
        for i in range(self.n):
            leader_buf[i]=self.leader(i)
            group_size[leader_buf[i]]+=1
        result=[[] for i in range(self.n)]
        for i in range(self.n):
            result[leader_buf[i]].append(i)
        result2=[]
        for i in range(self.n):
            if len(result[i])>0:
                result2.append(result[i])
        return result2

N, M = map(int, input().split())
tree = dsu(N)
mem = [[]for i in range(N)]
for i in range(M):
    r, c = map(int, input().split())
    mem[r-1].append(c-1)
for i in range(N):
    for j in range(1,len(mem[i])):
        tree.merge(mem[i][j], mem[i][j-1])
cnt = [0]*N
x = []
y = []
for i in range(N):
    if len(mem[i]) == 0:
        x.append(1)
        y.append(0)
    else:
        cnt[tree.leader(mem[i][0])] += 1
for i in range(N):
    if tree.leader(i) == i:
        x.append(cnt[i])
        y.append(tree.size(i))
inf = int(1e18)
ans = inf
K = len(x)
dp = [inf]*(N+1)
dp[0] = 0
for i in range(K):
    for j in reversed(range(N+1)):
        if j < x[i]:
            break
        dp[j] = min(dp[j], dp[j-x[i]]+y[i])
for i in range(N+1):
    if dp[i] == inf:
        continue
    ans = min(ans, N*N+2*i*dp[i]-dp[i]*N-i*N-M)
for i in range(K):
    x[i], y[i] = y[i], x[i]
dp = [inf]*(N+1)
dp[0] = 0
for i in range(K):
    for j in reversed(range(N+1)):
        if j < x[i]:
            break
        dp[j] = min(dp[j], dp[j-x[i]]+y[i])
for i in range(N+1):
    if dp[i] == inf:
        continue
    ans = min(ans, N*N+2*i*dp[i]-dp[i]*N-i*N-M)
print(ans)
0