結果
問題 | No.1615 Double Down |
ユーザー | iaNTU |
提出日時 | 2021-06-22 03:18:05 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 8,695 ms / 10,000 ms |
コード長 | 6,254 bytes |
コンパイル時間 | 460 ms |
コンパイル使用メモリ | 82,424 KB |
実行使用メモリ | 260,796 KB |
最終ジャッジ日時 | 2024-07-17 15:37:48 |
合計ジャッジ時間 | 42,065 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,588 ms
158,468 KB |
testcase_01 | AC | 1,597 ms
159,776 KB |
testcase_02 | AC | 1,958 ms
189,124 KB |
testcase_03 | AC | 1,983 ms
189,336 KB |
testcase_04 | AC | 2,175 ms
200,536 KB |
testcase_05 | AC | 2,133 ms
204,780 KB |
testcase_06 | AC | 2,742 ms
246,232 KB |
testcase_07 | AC | 2,649 ms
248,308 KB |
testcase_08 | AC | 2,614 ms
244,596 KB |
testcase_09 | AC | 7,309 ms
240,968 KB |
testcase_10 | AC | 7,389 ms
244,852 KB |
testcase_11 | AC | 6,685 ms
242,908 KB |
testcase_12 | AC | 3,769 ms
254,300 KB |
testcase_13 | AC | 3,707 ms
254,732 KB |
testcase_14 | AC | 3,571 ms
252,468 KB |
testcase_15 | AC | 3,058 ms
258,400 KB |
testcase_16 | AC | 3,022 ms
260,796 KB |
testcase_17 | AC | 2,954 ms
259,960 KB |
testcase_18 | AC | 6,716 ms
224,760 KB |
testcase_19 | AC | 8,523 ms
232,852 KB |
testcase_20 | AC | 8,126 ms
226,692 KB |
testcase_21 | AC | 8,110 ms
235,476 KB |
testcase_22 | AC | 8,616 ms
242,984 KB |
testcase_23 | AC | 8,695 ms
239,572 KB |
testcase_24 | AC | 7,879 ms
239,600 KB |
testcase_25 | AC | 7,071 ms
239,472 KB |
testcase_26 | AC | 7,240 ms
238,888 KB |
testcase_27 | AC | 530 ms
92,048 KB |
testcase_28 | AC | 491 ms
92,360 KB |
testcase_29 | AC | 581 ms
95,280 KB |
testcase_30 | AC | 565 ms
95,568 KB |
testcase_31 | AC | 601 ms
96,912 KB |
testcase_32 | AC | 633 ms
98,948 KB |
testcase_33 | AC | 794 ms
103,300 KB |
testcase_34 | AC | 763 ms
102,756 KB |
testcase_35 | AC | 787 ms
102,900 KB |
testcase_36 | AC | 148 ms
79,228 KB |
testcase_37 | AC | 153 ms
79,240 KB |
testcase_38 | AC | 169 ms
79,744 KB |
testcase_39 | AC | 173 ms
79,864 KB |
testcase_40 | AC | 162 ms
79,132 KB |
testcase_41 | AC | 163 ms
78,808 KB |
testcase_42 | AC | 190 ms
80,020 KB |
testcase_43 | AC | 203 ms
80,388 KB |
testcase_44 | AC | 194 ms
80,004 KB |
testcase_45 | AC | 45 ms
56,380 KB |
testcase_46 | AC | 44 ms
57,288 KB |
testcase_47 | AC | 47 ms
57,116 KB |
testcase_48 | AC | 44 ms
57,360 KB |
testcase_49 | AC | 44 ms
55,928 KB |
testcase_50 | AC | 44 ms
56,944 KB |
testcase_51 | AC | 43 ms
57,608 KB |
testcase_52 | AC | 45 ms
55,684 KB |
testcase_53 | AC | 45 ms
56,776 KB |
testcase_54 | AC | 45 ms
56,536 KB |
testcase_55 | AC | 45 ms
58,180 KB |
testcase_56 | AC | 46 ms
56,884 KB |
ソースコード
from collections import deque class mf_graph: n=1 g=[[] for i in range(1)] pos=[] def __init__(self,N): self.n=N self.g=[[] for i in range(N)] self.pos = [] def add_edge(self,From,To,cap): assert 0<=From and From<self.n assert 0<=To and To<self.n assert 0<=cap m=len(self.pos) self.pos.append((From,len(self.g[From]))) self.g[From].append({"to":To,"rev":len(self.g[To]),"cap":cap}) self.g[To].append({"to":From,"rev":len(self.g[From])-1,"cap":0}) return m def get_edge(self,i): m=len(self.pos) assert 0<=i and i<m _e=self.g[self.pos[i][0]][self.pos[i][1]] _re=self.g[_e["to"]][_e["rev"]] return {"from":self.pos[i][0], "to":_e["to"], "cap":_e["cap"]+_re["cap"], "flow":_re["cap"]} def edges(self): m=len(self.pos) result=[] for i in range(m): result.append(self.get_edge(i)) return result def change_edge(self,i,new_cap,new_flow): m=len(self.pos) assert 0<=i and i<m assert 0<=new_flow and new_flow<=new_cap _e=self.g[self.pos[i][0]][self.pos[i][1]] _re=self.g[_e["to"]][_e["rev"]] _e["cap"]=new_cap-new_flow _re["cap"]=new_flow def flow(self,s,t,flow_limit=(2**31)-1): assert 0<=s and s<self.n assert 0<=t and t<self.n level=[0 for i in range(self.n)] Iter=[0 for i in range(self.n)] que=deque([]) def bfs(): for i in range(self.n):level[i]=-1 level[s]=0 que=deque([]) que.append(s) while(len(que)>0): v=que.popleft() for e in self.g[v]: if e["cap"]==0 or level[e["to"]]>=0:continue level[e["to"]]=level[v]+1 if e["to"]==t:return que.append(e["to"]) def dfs(func,v,up): if (v==s):return up res=0 level_v=level[v] for i in range(Iter[v],len(self.g[v])): e=self.g[v][i] if (level_v<=level[e["to"]] or self.g[e["to"]][e["rev"]]["cap"]==0):continue d=func(func,e["to"],min(up-res,self.g[e["to"]][e["rev"]]["cap"])) if d<=0:continue self.g[v][i]["cap"]+=d self.g[e["to"]][e["rev"]]["cap"]-=d res+=d if res==up:return res level[v]=self.n return res flow=0 while(flow<flow_limit): bfs() if level[t]==-1:break for i in range(self.n):Iter[i]=0 while(flow<flow_limit): f=dfs(dfs,t,flow_limit-flow) if not(f):break flow+=f return flow def min_cut(self,s): visited=[False for i in range(self.n)] que=deque([]) que.append(s) while(len(que)>0): p=que.popleft() visited[p]=True for e in self.g[p]: if e["cap"] and not(visited[e["to"]]): visited[e["to"]]=True que.append(e["to"]) return visited 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] * (n + 1) alivey = [True] * (m + 1) wentx = [False] * (n + 1) wenty = [False] * (m + 1) val = [0] * (k + 1) matchx = [0] * (n + 1) matchy = [0] * (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, 1) for j in range(1, m + 1) : G.add_edge(j + n, T, 1) previous_edges = [] 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, 1) 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]) val[i] = G.flow(S, T) for j in range(1, n + 1) : matchx[j] = -1 for j in range(1, m + 1) : matchy[j] = -1 for j in range(1, n + 1) : wentx[j] = False for j in range(1, m + 1) : wenty[j] = False for j in range(1, n + 1) : for e in G.g[j] : if e["to"] != S and e["cap"] == 0 and G.g[e["to"]][e["rev"]]["cap"] == 1 : matchx[j] = e["to"] - n matchy[e["to"] - n] = j qu = [] for j in range(1, n + 1) : if matchx[j] < 0 : wentx[j] = True qu.append(j) while len(qu) : idx = qu.pop() for j in gx[idx] : if not wentx[matchy[j]] : wentx[matchy[j]] = True 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.append(j) while len(qu) : idx = qu.pop() for j in gy[idx] : if not wenty[matchx[j]] : wenty[matchx[j]] = True qu.append(matchx[j]) for j in range(1, m + 1) : if not wenty[j] : alivey[j] = False gsz = [len(G.g[j]) for j in range(0, n + 1)] for j in range(1, n + 1) : if not wentx[j] : for idx in range(gsz[j]) : e = G.g[j][idx] if e["cap"] >= 0 and e["to"] != S and not wenty[e["to"] - n] and matchx[j] != e["to"] - n : G.g[j][idx]["cap"] = 0 G.g[e["to"]][e["rev"]]["cap"] = 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 ans = 0 for i in range(0, k + 1) : ans += val[i] << i print(ans)