結果

問題 No.1615 Double Down
ユーザー iaNTUiaNTU
提出日時 2021-06-23 03:06:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 7,710 ms / 10,000 ms
コード長 4,701 bytes
コンパイル時間 584 ms
コンパイル使用メモリ 82,452 KB
実行使用メモリ 266,316 KB
最終ジャッジ日時 2024-07-17 15:47:23
合計ジャッジ時間 117,934 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 940 ms
156,928 KB
testcase_01 AC 1,019 ms
158,828 KB
testcase_02 AC 1,427 ms
204,768 KB
testcase_03 AC 1,381 ms
204,404 KB
testcase_04 AC 1,084 ms
185,344 KB
testcase_05 AC 1,043 ms
182,656 KB
testcase_06 AC 1,661 ms
246,052 KB
testcase_07 AC 1,688 ms
247,096 KB
testcase_08 AC 1,703 ms
245,016 KB
testcase_09 AC 6,372 ms
238,828 KB
testcase_10 AC 6,214 ms
240,148 KB
testcase_11 AC 6,431 ms
238,308 KB
testcase_12 AC 2,886 ms
257,876 KB
testcase_13 AC 2,846 ms
255,888 KB
testcase_14 AC 2,725 ms
256,040 KB
testcase_15 AC 1,956 ms
264,640 KB
testcase_16 AC 1,976 ms
266,316 KB
testcase_17 AC 1,984 ms
263,628 KB
testcase_18 AC 6,947 ms
251,820 KB
testcase_19 AC 7,710 ms
250,280 KB
testcase_20 AC 7,498 ms
255,412 KB
testcase_21 AC 7,235 ms
248,724 KB
testcase_22 AC 6,592 ms
243,340 KB
testcase_23 AC 7,414 ms
245,500 KB
testcase_24 AC 6,994 ms
245,504 KB
testcase_25 AC 6,681 ms
250,632 KB
testcase_26 AC 6,763 ms
245,288 KB
testcase_27 AC 323 ms
85,020 KB
testcase_28 AC 312 ms
85,772 KB
testcase_29 AC 393 ms
91,828 KB
testcase_30 AC 396 ms
91,560 KB
testcase_31 AC 354 ms
87,624 KB
testcase_32 AC 370 ms
88,648 KB
testcase_33 AC 483 ms
97,428 KB
testcase_34 AC 484 ms
97,428 KB
testcase_35 AC 508 ms
98,288 KB
testcase_36 AC 122 ms
78,140 KB
testcase_37 AC 128 ms
77,932 KB
testcase_38 AC 141 ms
78,316 KB
testcase_39 AC 141 ms
78,080 KB
testcase_40 AC 147 ms
78,080 KB
testcase_41 AC 145 ms
77,952 KB
testcase_42 AC 154 ms
78,208 KB
testcase_43 AC 154 ms
78,208 KB
testcase_44 AC 156 ms
78,304 KB
testcase_45 AC 38 ms
53,376 KB
testcase_46 AC 40 ms
53,632 KB
testcase_47 AC 43 ms
54,272 KB
testcase_48 AC 39 ms
53,632 KB
testcase_49 AC 39 ms
52,992 KB
testcase_50 AC 39 ms
53,248 KB
testcase_51 AC 39 ms
53,504 KB
testcase_52 AC 40 ms
53,760 KB
testcase_53 AC 41 ms
53,888 KB
testcase_54 AC 40 ms
53,632 KB
testcase_55 AC 41 ms
54,144 KB
testcase_56 AC 41 ms
53,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class mf_graph:
    def __init__(self, N) :
        self.size = N
        self.g = [[] for i in range(self.size)]
        self.gsz = [0 for i in range(self.size)]

    def add_edge(self, u, v) :
        self.g[u].append([v, self.gsz[v], 1])
        self.g[v].append([u, self.gsz[u], 0])
        self.gsz[u] += 1
        self.gsz[v] += 1

    def Bfs(self, s, t) :
        q = [s]
        idx = 0
        q_sz = 1
        self.went[s] = t
        self.Iter[s] = self.dep[s] = 0
        while idx < q_sz :
            now = q[idx]
            idx += 1
            for i in self.g[now] :
                if i[2] > 0 and self.went[i[0]] != t :
                    self.went[i[0]] = t
                    self.dep[i[0]] = self.dep[now] + 1
                    self.Iter[i[0]] = 0
                    q_sz += 1
                    q.append(i[0])

    def Dfs(self, u, t, nv) :
        if u == t :
            return nv
        while self.Iter[u] < self.gsz[u] :
            i = self.Iter[u]
            if self.g[u][i][2] > 0 and self.dep[self.g[u][i][0]] > self.dep[u] :
                if self.Dfs(self.g[u][i][0], t, nv) > 0 :
                    self.g[u][i][2] = 0
                    self.g[self.g[u][i][0]][self.g[u][i][1]][2] = 1
                    return 1
            self.Iter[u] += 1
        return 0

    def flow(self, s, t) :
        self.dep = [0 for i in range(self.size)]
        self.went = [0 for i in range(self.size)]
        self.Iter = [0 for i in range(self.size)]
        cnt = 0; ans = 0; kInf = 1 << 30
        while True :
            cnt += 1
            self.Bfs(s, cnt)
            if self.went[s] != self.went[t] :
                break
            f = 0
            while True :
                f = self.Dfs(s, t, kInf)
                if f > 0 :
                    ans += f
                else :
                    break
        return ans

n, m, k, l = map(int, input().split())
x = [0] * l; y = [0] * l; z = [0] * l
for i in range(l) :
    x[i], y[i], z[i] = map(int, input().split())

edges = [[] for i in range(k + 1)]
for i in range(l) :
    edges[z[i]].append(i)

alivex = [True for i in range(n + 1)]
alivey = [True for i in range(m + 1)]

S = 0; T = n + m + 1
G = mf_graph(n + m + 2)
for j in range(1, n + 1) :
    G.add_edge(S, j)
for j in range(1, m + 1) :
    G.add_edge(j + n, T)

previous_edges = []
qu = []
qu_sz = 0
ans = 0

for i in range(k, -1, -1) :
    for j in edges[i] :
        if alivex[x[j]] and alivey[y[j]] : 
            previous_edges.append(j)
            G.add_edge(x[j], y[j] + n)

    gx = [[] for j in range(n + 1)]
    gy = [[] for j in range(m + 1)]

    for j in previous_edges :
        gx[x[j]].append(y[j])
        gy[y[j]].append(x[j])

    ans += G.flow(S, T) << i

    wentx = [False for i in range(n + 1)]
    wenty = [False for i in range(m + 1)]
    matchx = [-1 for i in range(n + 1)]
    matchy = [-1 for i in range(m + 1)]
    
    for j in range(1, n + 1) :
        for e in G.g[j] :
            if e[0] != S and e[2] == 0 and G.g[e[0]][e[1]][2] == 1 :
                matchx[j] = e[0] - n
                matchy[e[0] - n] = j

    for j in range(1, n + 1) :
        if matchx[j] < 0 :
            wentx[j] = True
            qu_sz += 1
            qu.append(j)
    
    while qu_sz > 0 :
        idx = qu.pop()
        qu_sz -= 1
        for j in gx[idx] :
            if not wentx[matchy[j]] :
                wentx[matchy[j]] = True
                qu_sz += 1
                qu.append(matchy[j])

    for j in range(1, n + 1) :
        if not wentx[j] :
            alivex[j] = False

    for j in range(1, m + 1) :
        if matchy[j] < 0 :
            wenty[j] = True
            qu_sz += 1
            qu.append(j)

    while qu_sz > 0 :
        idx = qu.pop()
        qu_sz -= 1
        for j in gy[idx] :
            if not wenty[matchx[j]] :
                wenty[matchx[j]] = True
                qu_sz += 1
                qu.append(matchx[j])

    for j in range(1, m + 1) :
        if not wenty[j] :
            alivey[j] = False

    for j in range(1, n + 1) :
        if not wentx[j] :
            for idx in range(G.gsz[j]) :
                e = G.g[j][idx]
                if e[2] >= 0 and e[0] != S and not wenty[e[0] - n] and matchx[j] != e[0] - n :
                    G.g[j][idx][2] = 0
                    G.g[e[0]][e[1]][2] = 0

    psz = len(previous_edges)
    j = 0
    while j < psz :
        idx = previous_edges[j]
        if not wentx[x[idx]] and not wenty[y[idx]] and matchx[x[idx]] != y[idx] :
            psz -= 1
            previous_edges[j], previous_edges[psz] = previous_edges[psz], previous_edges[j]
            previous_edges.pop()
            j -= 1
        j += 1
print(ans)
0