提出 #17017372


ソースコード 拡げる

from collections import deque

class MaxFlow:
    inf = 10**18

    class E:
        def __init__(self,to,cap):
            self.to = to
            self.cap = cap
            self.rev = None

    def __init__(self,n):
        self.n = n
        self.graph = [[] for _ in range(n)]

    def add_edge(self, fr, to, cap):
        graph = self.graph
        edge = self.E(to,cap)
        edge2 = self.E(fr,0)
        edge.rev = edge2
        edge2.rev = edge
        graph[fr].append(edge)
        graph[to].append(edge2)

    def bfs(self, s, t):
        level = self.level = [self.n]*self.n
        q = deque([s])
        level[s] = 0
        while q:
            now = q.popleft()
            lw = level[now]+1
            for e in self.graph[now]:
                if e.cap and level[e.to]> lw:
                    level[e.to] = lw
                    if e.to == t:
                        return True
                    q.append(e.to)
        return False

    def dfs(self, s, t, up):
        graph = self.graph
        it = self.it
        level = self.level

        st = deque([t])
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    e = graph[w][it[w]].rev
                    flow = min(flow, e.cap)
                for w in st:
                    e = graph[w][it[w]]
                    e.cap += flow
                    e.rev.cap -= flow
                return flow
            lv = level[v]-1
            while it[v] < len(graph[v]):
                e = graph[v][it[v]]
                re = e.rev
                if re.cap == 0 or lv != level[e.to]:
                    it[v] += 1
                    continue
                st.append(e.to)
                break
            if it[v] == len(graph[v]):
                st.pop()
                level[v] = self.n

        return 0

    def flow(self,s,t,flow_limit=inf):
        flow = 0
        while flow < flow_limit and self.bfs(s,t):
            self.it = [0]*self.n
            while flow < flow_limit:
                f = self.dfs(s,t,flow_limit-flow)
                if f == 0:
                    break
                flow += f
        return flow

    def min_cut(self,s):
        visited = [False]*self.n
        q = deque([s])
        while q:
            v = q.pop()
            visited[v] = True
            for e in self.graph[v]:
                if e.cap and not visited[e.to]:
                    q.append(e.to)
        return visited

n,m = map(int,input().split())
S = [list(input()) for i in range(n)]

s = n*m
t = s+1
flow = MaxFlow(t+1)
for i in range(n):
    for j in range(m):
        if S[i][j] == "#":
            continue
        if (i+j) % 2 == 0:
            flow.add_edge(s,i*m+j,1)
        else:
            flow.add_edge(i*m+j,t,1)

for i in range(n):
    for j in range(m):
        if S[i][j] != ".":
            continue
        if j+1 < m and S[i][j+1] == ".":
            u,v = m*i+j,m*i+j+1
            if (i+j) % 2 == 1:
                u,v = v,u
            flow.add_edge(u, v, 1)
        if i+1 < n and S[i+1][j] == ".":
            u,v = m*i+j, m*(i+1)+j
            if (i+j) % 2 == 1:
                u,v = v,u
            flow.add_edge(u,v,1)
print(flow.flow(s,t))
for u in range(s):
    for e in flow.graph[u]:
        v = e.to
        ui, uj = divmod(u, m)
        vi, vj = divmod(v, m)
        if (ui+uj) % 2 == 0 and e.cap == 0 and v != s and v != t:
            if ui+1 == vi:
                S[ui][uj] = "v"
                S[vi][vj] = "^"
            elif ui == vi+1:
                S[ui][uj] = "^"
                S[vi][vj] = "v"
            elif uj+1 == vj:
                S[ui][uj] = ">"
                S[vi][vj] = "<"
            elif uj == vj+1:
                S[ui][uj] = "<"
                S[vi][vj] = ">"
        
for i in S:
    print(*i,sep="")

提出情報

提出日時
問題 D - Maxflow
ユーザ aaaaaaaaaa2230
言語 PyPy3 (7.3.0)
得点 100
コード長 4028 Byte
結果 AC
実行時間 205 ms
メモリ 77136 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 100 / 100
結果
AC × 1
AC × 19
セット名 テストケース
Sample 00-sample-01.txt
All 00-sample-01.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt
ケース名 結果 実行時間 メモリ
00-sample-01.txt AC 80 ms 67316 KiB
01-01.txt AC 66 ms 67276 KiB
01-02.txt AC 66 ms 67360 KiB
01-03.txt AC 64 ms 67268 KiB
01-04.txt AC 61 ms 67052 KiB
01-05.txt AC 72 ms 67476 KiB
01-06.txt AC 70 ms 67372 KiB
01-07.txt AC 158 ms 75608 KiB
01-08.txt AC 84 ms 68596 KiB
01-09.txt AC 73 ms 67656 KiB
01-10.txt AC 108 ms 69756 KiB
01-11.txt AC 82 ms 68448 KiB
01-12.txt AC 163 ms 77136 KiB
01-13.txt AC 205 ms 76448 KiB
01-14.txt AC 183 ms 74096 KiB
01-15.txt AC 144 ms 72000 KiB
01-16.txt AC 80 ms 68772 KiB
01-17.txt AC 176 ms 74752 KiB
01-18.txt AC 175 ms 74632 KiB