Problem:


Score : 100 points

Problem Statement

You are given a grid of N rows and M columns. The square at the i-th row and j-th column will be denoted as (i,j).
Some of the squares contain an object. All the remaining squares are empty.
The state of the grid is represented by strings S_1,S_2,\cdots,S_N. The square (i,j) contains an object if S_{i,j}= # and is empty if S_{i,j}= ..

Consider placing 1 \times 2 tiles on the grid. Tiles can be placed vertically or horizontally to cover two adjacent empty squares.
Tiles must not stick out of the grid, and no two different tiles may intersect. Tiles cannot occupy the square with an object.

Calculate the maximum number of tiles that can be placed and any configulation that acheives the maximum.

Constraints

1 \leq N \leq 100

1 \leq M \leq 100

S_i is a string with length M consists of # and ..

Input

Input is given from Standard Input in the following format:

N M
S_1
S_2
\vdots
S_N

Output

On the first line, print the maximum number of tiles that can be placed.

On the next N lines, print a configulation that achieves the maximum.
Precisely, output the strings t_1,t_2,\cdots,t_N constructed by the following way.

t_i is initialized to S_i.

For each (i,j), if there is a tile that occupies (i,j) and (i+1,j), change t_{i,j}:=v, t_{i+1,j}:=^.

For each (i,j), if there is a tile that occupies (i,j) and (i,j+1), change t_{i,j}:=>, t_{i,j+1}:=<.

See samples for further information.

You may print any configulation that maximizes the number of tiles.

Sample Input 1

3 3
#..
..#
...

Sample Output 1

3
#><
vv#
^^.

The following output is also treated as a correct answer.

3
#><
v.#
^><



Sample input:
3 3
#..
..#
...


Sample output:
3
#><
vv#
^^.

Submission status: Accepted

Solution code:
class mf_graph:
    def __init__(self, n=0):
        self._n = n
        self.g = [[] for _ in range(n)]
        self.pos = []
    
    def add_edge(self, frm, to, cap):
        m = len(self.pos)
        e1 = mf_graph._edge(to, cap)
        e2 = mf_graph._edge(frm, 0)
        e1.rev = e2
        e2.rev = e1
        self.pos.append(e1)
        self.g[frm].append(e1)
        self.g[to].append(e2)
        return m
    
    class edge:
        def __init__(self, frm, to, cap, flow):
            self.frm = frm
            self.to = to
            self.cap = cap
            self.flow = flow
        
        def __iter__(self):
            yield self.frm
            yield self.to
            yield self.cap
            yield self.flow
    
    def get_edge(self, i):
        e1 = self.pos[i]
        e2 = e1.rev
        return mf_graph.edge(e2.to, e1.to, e1.cap + e2.cap, e2.cap)
    
    def edges(self):
        return [self.get_edge(i) for i in range(len(self.pos))]
    
    def change_edge(self, i, new_cap, new_flow):
        e = self.pos[i]
        e.cap = new_cap - new_flow
        e.rev.cap = new_flow
    
    def flow(self, s, t, flow_limit=0XFFFFFFFFFFFFFFF):
        g = self.g
        flow = 0
        que = [None] * self._n
        while flow < flow_limit:
            level = [-1] * self._n
            level[s] = 0
            ql = 0
            qr = 1
            que[0] = s
            unreached = s != t
            while unreached and ql < qr:
                v = que[ql]
                ql += 1
                for e in g[v]:
                    to = e.to
                    if e.cap and level[to] < 0:
                        level[to] = level[v] + 1
                        if to == t:
                            unreached = False
                            break
                        que[qr] = to
                        qr += 1
            if unreached:
                return flow
            ptr = [len(es) for es in g]
            stack = []
            v = t
            up = flow_limit - flow
            res = 0
            while True:
                if v == s or not ptr[v]:
                    if v == s:
                        res = up
                    while stack:
                        tmp = res
                        e, up, res = stack.pop()
                        e.cap -= tmp
                        e.rev.cap += tmp
                        res += tmp
                        if res < up:
                            v = e.to
                            break
                    else:
                        flow += res
                        break
                i = ptr[v]
                while i:
                    i -= 1
                    e = g[v][i]
                    if level[e.to] == level[v] - 1 and e.rev.cap:
                        ptr[v] = i
                        stack.append((e.rev, up, res))
                        v = e.to
                        up = min(up, e.rev.cap)
                        res = 0
                        break
                else:
                    ptr[v] = i
        return flow
    
    def min_cut(self, s):
        visited = [False] * self._n
        que = [None] * self._n
        ql = 0
        qr = 1
        que[0] = s
        visited[s] = True
        while ql < qr:
            p = que[ql]
            ql += 1
            for e in self.g[p]:
                if e.cap and not visited[e.to]:
                    visited[e.to] = True
                    que[qr] = e.to
                    qr += 1
        return visited
    
    class _edge:
        def __init__(self, to, cap):
            self.to = to
            self.cap = cap

n, m = map(int, input().split())
s = [input() for _ in range(n)]
g = mf_graph(n * m + 2)
for i in range(n):
    for j in range(i % 2, m, 2):
        if s[i][j] == '#':
            continue
        for di, dj in (1, 0), (0, 1), (-1, 0), (0, -1):
            x, y = i + di, j + dj
            if 0 <= x < n and 0 <= y < m and s[x][y] == '.':
                g.add_edge(i * m + j, x * m + y, 1)
for i in range(n):
    for j in range(m):
        if s[i][j] == '#':
            continue
        if (i + j) % 2 == 0:
            g.add_edge(n * m, i * m + j, 1)
        else:
            g.add_edge(i * m + j, n * m + 1, 1)
print(g.flow(n * m, n * m + 1))
ans = [list(si) for si in s]
for frm, to, cap, flow in g.edges():
    if flow and frm < n * m and to < n * m:
        i, j = frm // m, frm % m
        x, y = to // m, to % m
        if j == y:
            if i > x:
                i, j, x, y = x, y, i, j
            ans[i][j], ans[x][y] = 'v^'
        else:
            if j > y:
                i, j, x, y = x, y, i, j
            ans[i][j], ans[x][y] = '><'

for a in ans:
    print(''.join(a))

Submission status: Accepted

Solution code:
class mf_graph:
    def __init__(self, n=0):
        self._n = n
        self.g = [[] for _ in range(n)]
        self.pos = []
    
    def add_edge(self, frm, to, cap):
        m = len(self.pos)
        e1 = mf_graph._edge(to, cap)
        e2 = mf_graph._edge(frm, 0)
        e1.rev = e2
        e2.rev = e1
        self.pos.append(e1)
        self.g[frm].append(e1)
        self.g[to].append(e2)
        return m
    
    class edge:
        def __init__(self, frm, to, cap, flow):
            self.frm = frm
            self.to = to
            self.cap = cap
            self.flow = flow
        
        def __iter__(self):
            yield self.frm
            yield self.to
            yield self.cap
            yield self.flow
    
    def get_edge(self, i):
        e1 = self.pos[i]
        e2 = e1.rev
        return mf_graph.edge(e2.to, e1.to, e1.cap + e2.cap, e2.cap)
    
    def edges(self):
        return [self.get_edge(i) for i in range(len(self.pos))]
    
    def change_edge(self, i, new_cap, new_flow):
        e = self.pos[i]
        e.cap = new_cap - new_flow
        e.rev.cap = new_flow
    
    def flow(self, s, t, flow_limit=0XFFFFFFFFFFFFFFF):
        g = self.g
        flow = 0
        es = [[] for _ in range(self._n)]
        while flow < flow_limit:
            level = [-1] * self._n
            level[s] = 0
            que = [None] * self._n
            ql = 0
            qr = 1
            que[0] = s
            unreached = s != t
            lvl = 1
            for p in es:
                p.clear()
            while unreached and ql < qr:
                nqr = qr
                while ql < qr:
                    v = que[ql]
                    ql += 1
                    for e in g[v]:
                        to = e.to
                        if e.cap and not 0 <= level[to] < lvl:
                            es[to].append(e.rev)
                            if level[to] < 0:
                                level[to] = lvl
                                if to == t:
                                    unreached = False
                                que[nqr] = to
                                nqr += 1
                qr = nqr
                lvl += 1
            if unreached:
                return flow
            stack = []
            v = t
            up = flow_limit - flow
            res = 0
            while True:
                if v == s or not es[v]:
                    if v == s:
                        res = up
                    while stack:
                        tmp = res
                        e, up, res = stack.pop()
                        e.cap -= tmp
                        e.rev.cap += tmp
                        res += tmp
                        if res < up:
                            v = e.to
                            break
                    else:
                        flow += res
                        break
                else:
                    e = es[v].pop()
                    stack.append((e.rev, up, res))
                    v = e.to
                    up = min(up, e.rev.cap)
                    res = 0
        return flow
    
    def min_cut(self, s):
        visited = [False] * self._n
        que = [None] * self._n
        ql = 0
        qr = 1
        que[0] = s
        visited[s] = True
        while ql < qr:
            p = que[ql]
            ql += 1
            for e in self.g[p]:
                if e.cap and not visited[e.to]:
                    visited[e.to] = True
                    que[qr] = e.to
                    qr += 1
        return visited
    
    class _edge:
        def __init__(self, to, cap):
            self.to = to
            self.cap = cap


n, m = map(int, input().split())
s = [input() for _ in range(n)]
g = mf_graph(n * m + 2)
for i in range(n):
    for j in range(i % 2, m, 2):
        if s[i][j] == '#':
            continue
        for di, dj in (1, 0), (0, 1), (-1, 0), (0, -1):
            x, y = i + di, j + dj
            if 0 <= x < n and 0 <= y < m and s[x][y] == '.':
                g.add_edge(i * m + j, x * m + y, 1)
for i in range(n):
    for j in range(m):
        if s[i][j] == '#':
            continue
        if (i + j) % 2 == 0:
            g.add_edge(n * m, i * m + j, 1)
        else:
            g.add_edge(i * m + j, n * m + 1, 1)
print(g.flow(n * m, n * m + 1))
ans = [list(si) for si in s]
for frm, to, cap, flow in g.edges():
    if flow and frm < n * m and to < n * m:
        i, j = frm // m, frm % m
        x, y = to // m, to % m
        if j == y:
            if i > x:
                i, j, x, y = x, y, i, j
            ans[i][j], ans[x][y] = 'v^'
        else:
            if j > y:
                i, j, x, y = x, y, i, j
            ans[i][j], ans[x][y] = '><'

for a in ans:
    print(''.join(a))

Submission status: Accepted

Solution code:
class mf_graph:
    def __init__(self, n=0):
        self._n = n
        self.g = [[] for _ in range(n)]
        self.pos = []
    
    def add_edge(self, frm, to, cap):
        m = len(self.pos)
        e1 = mf_graph._edge(to, cap)
        e2 = mf_graph._edge(frm, 0)
        e1.rev = e2
        e2.rev = e1
        self.pos.append(e1)
        self.g[frm].append(e1)
        self.g[to].append(e2)
        return m
    
    class edge:
        def __init__(self, frm, to, cap, flow):
            self.frm = frm
            self.to = to
            self.cap = cap
            self.flow = flow
        
        def __iter__(self):
            yield self.frm
            yield self.to
            yield self.cap
            yield self.flow
    
    def get_edge(self, i):
        e1 = self.pos[i]
        e2 = e1.rev
        return mf_graph.edge(e2.to, e1.to, e1.cap + e2.cap, e2.cap)
    
    def edges(self):
        return [self.get_edge(i) for i in range(len(self.pos))]
    
    def change_edge(self, i, new_cap, new_flow):
        e = self.pos[i]
        e.cap = new_cap - new_flow
        e.rev.cap = new_flow
    
    def flow(self, s, t, flow_limit=0XFFFFFFFFFFFFFFF):
        g = self.g
        flow = 0
        while flow < flow_limit:
            level = [-1] * self._n
            level[s] = 0
            que = [None] * self._n
            ql = 0
            qr = 1
            que[0] = s
            unreached = True
            while unreached and ql < qr:
                v = que[ql]
                ql += 1
                for e in g[v]:
                    to = e.to
                    if e.cap and level[to] < 0:
                        level[to] = level[v] + 1
                        if to == t:
                            unreached = False
                            break
                        que[qr] = to
                        qr += 1
            if unreached:
                return flow
            ptr = [len(es) for es in g]
            stack = []
            v = t
            up = flow_limit - flow
            res = 0
            while True:
                if v == s or not ptr[v]:
                    if v == s:
                        res = up
                    while stack:
                        tmp = res
                        e, up, res = stack.pop()
                        e.cap -= tmp
                        e.rev.cap += tmp
                        res += tmp
                        if res < up:
                            v = e.to
                            break
                    else:
                        flow += res
                        break
                i = ptr[v]
                while i:
                    i -= 1
                    e = g[v][i]
                    if level[e.to] == level[v] - 1 and e.rev.cap:
                        ptr[v] = i
                        stack.append((e.rev, up, res))
                        v = e.to
                        up = min(up, e.rev.cap)
                        res = 0
                        break
                else:
                    ptr[v] = i
        return flow
    
    def min_cut(self, s):
        visited = [False] * self._n
        que = [None] * self._n
        ql = 0
        qr = 1
        que[0] = s
        visited[s] = True
        while ql < qr:
            p = que[ql]
            ql += 1
            for e in self.g[p]:
                if e.cap and not visited[e.to]:
                    visited[e.to] = True
                    que[qr] = e.to
                    qr += 1
        return visited
    
    class _edge:
        def __init__(self, to, cap):
            self.to = to
            self.cap = cap

n, m = map(int, input().split())
s = [input() for _ in range(n)]
g = mf_graph(n * m + 2)
for i in range(n):
    for j in range(i % 2, m, 2):
        if s[i][j] == '#':
            continue
        for di, dj in (1, 0), (0, 1), (-1, 0), (0, -1):
            x, y = i + di, j + dj
            if 0 <= x < n and 0 <= y < m and s[x][y] == '.':
                g.add_edge(i * m + j, x * m + y, 1)
for i in range(n):
    for j in range(m):
        if s[i][j] == '#':
            continue
        if (i + j) % 2 == 0:
            g.add_edge(n * m, i * m + j, 1)
        else:
            g.add_edge(i * m + j, n * m + 1, 1)
print(g.flow(n * m, n * m + 1))
ans = [list(si) for si in s]
for frm, to, cap, flow in g.edges():
    if flow and frm < n * m and to < n * m:
        i, j = frm // m, frm % m
        x, y = to // m, to % m
        if j == y:
            if i > x:
                i, j, x, y = x, y, i, j
            ans[i][j], ans[x][y] = 'v^'
        else:
            if j > y:
                i, j, x, y = x, y, i, j
            ans[i][j], ans[x][y] = '><'

for a in ans:
    print(''.join(a))

Submission status: Accepted

Solution code:
def FF(E, s, t, nn):
    G = [[] for _ in range(nn)]
    for a, b, f in E:
        G[a].append([b, f, len(G[b])])
        G[b].append([a, 0, len(G[a])-1])
    
    def dfs(s, t, f):
        if s == t:
            return f
        used[s] = 1
        for i, (b, _f, r) in enumerate(G[s]):
            if used[b] or _f == 0: continue
            d = dfs(b, t, min(f, _f))
            if d > 0:
                G[s][i][1] -= d
                G[b][r][1] += d
                return d
        return 0
    
    flow = 0
    while 1:
        used = [0] * nn
        f = dfs(s, t, 10**100)
        if f == 0:
            return flow, G
        flow += f

E = []
N, M = map(int, input().split())
A = [[a for a in input()] for _ in range(N)]
X = [[1 if a == "." else 0 for a in aa] for aa in A]
s = N * M
t = s + 1
for i in range(N):
    for j in range(M):
        if X[i][j]:
            if (i ^ j) & 1 == 0:
                E.append((s, i * M + j, 1))
            else:
                E.append((i * M + j, t, 1))

for i in range(N - 1):
    for j in range(M):
        if X[i][j] and X[i+1][j]:
            a = i * M + j
            b = a + M
            if (i ^ j) & 1 == 0:
                E.append((a, b, 1))
            else:
                E.append((b, a, 1))
for i in range(N):
    for j in range(M - 1):
        if X[i][j] and X[i][j+1]:
            a = i * M + j
            b = a + 1
            if (i ^ j) & 1 == 0:
                E.append((a, b, 1))
            else:
                E.append((b, a, 1))

D = {}
for a, b, v in E:
    D[(a << 16) + b] = v

ans, mf = FF(E, s, t, t + 1)
for a in range(t + 1):
    for b, w, _ in mf[a]:
        ab = (a << 16) + b
        if ab not in D: continue
        if w == 0:
            if a >= N * M or b >= N * M: continue
            aa, bb = a, b
            i, j = aa // M, aa % M
            ii, jj = bb // M, bb % M
            v = D[ab] - w
            aa, bb = (a, b) if a < b else (b, a)
            i, j = aa // M, aa % M
            ii, jj = bb // M, bb % M
            if i == ii:
                A[i][j] = ">"
                A[ii][jj] = "<"
            else:
                A[i][j] = "v"
                A[ii][jj] = "^"

print(ans)
for x in A:
    print("".join(map(str, x)))

Submission status: Accepted

Solution code:
import os
import sys

import numpy as np


def solve(n, m, sss):
    BIT_SHIFT = 9
    BIT_MASK = (1 << BIT_SHIFT) - 1

    def combine_rc(r, c):
        return (r << BIT_SHIFT) | c

    def separate_rc(rc):
        return rc >> BIT_SHIFT, rc & BIT_MASK

    DINIC_LINKS = []

    def dinic_init(n):
        lst = [[0, 0, 0]]
        lst.clear()
        DINIC_LINKS.append([lst.copy() for _ in range(n)])
        return len(DINIC_LINKS) - 1

    def dinic_add_link(ins, frm, to, cap):
        links = DINIC_LINKS[ins]
        links[frm].append([to, cap, len(links[to])])
        links[to].append([frm, 0, len(links[frm]) - 1])

    def dinic_bfs(ins, n, s):
        links = DINIC_LINKS[ins]
        depth = np.full(n, -1, dtype=np.int64)
        depth[s] = 0
        deq = np.zeros(n + 5, dtype=np.int64)
        dl, dr = 0, 1
        deq[0] = s
        while dl < dr:
            v = deq[dl]
            dl += 1
            for link in links[v]:
                if link[1] > 0 and depth[link[0]] == -1:
                    depth[link[0]] = depth[v] + 1
                    deq[dr] = link[0]
                    dr += 1
        return depth

    def dinic_dfs(ins, depth, progress, s, t):
        links = DINIC_LINKS[ins]
        stack = [(s, 10 ** 18)]
        flow = 0
        while stack:
            v, f = stack.pop()
            if v == t:
                flow = f
                continue
            if flow == 0:
                i = progress[v]
                if i == len(links[v]):
                    continue
                progress[v] += 1
                stack.append((v, f))
                to, cap, rev = links[v][i]
                if cap == 0 or depth[v] >= depth[to]:
                    continue
                stack.append((to, min(f, cap)))
            else:
                i = progress[v] - 1
                link = links[v][i]
                link[1] -= flow
                links[link[0]][link[2]][1] += flow
        return flow

    def dinic_maximum_flow(ins, n, s, t):
        flow = 0
        while True:
            depth = dinic_bfs(ins, n, s)
            if depth[t] == -1:
                return flow
            progress = np.zeros(n, dtype=np.int64)
            path_flow = dinic_dfs(ins, depth, progress, s, t)
            while path_flow != 0:
                flow += path_flow
                path_flow = dinic_dfs(ins, depth, progress, s, t)

    lefts = []
    rights = []
    rights_rev = {}

    for r in range(1, n + 1):
        for c in range(1, m + 1):
            if sss[r, c]:
                continue
            rc = combine_rc(r, c)
            if (r ^ c) & 1:
                lefts.append(rc)
            else:
                rights_rev[rc] = len(rights)
                rights.append(rc)
    l_size = len(lefts)
    r_size = len(rights)
    s = l_size + r_size
    t = s + 1
    total_size = t + 1

    ins = dinic_init(total_size)

    MOVE = (-(1 << BIT_SHIFT), -1, 1, 1 << BIT_SHIFT)

    for i in range(l_size):
        rc = lefts[i]
        for drc in MOVE:
            nrc = rc + drc
            if nrc in rights_rev:
                dinic_add_link(ins, i, rights_rev[nrc] + l_size, 1)
        dinic_add_link(ins, s, i, 1)
    for j in range(r_size):
        dinic_add_link(ins, j + l_size, t, 1)

    flow = dinic_maximum_flow(ins, total_size, s, t)
    links = DINIC_LINKS[ins]

    for i in range(l_size):
        for link in links[i]:
            if link[0] == s or link[1] == 1:
                continue
            j = link[0] - l_size
            ir, ic = separate_rc(lefts[i])
            jr, jc = separate_rc(rights[j])
            if ir == jr:
                if ic > jc:
                    ic, jc = jc, ic
                sss[ir, ic] = 4
                sss[jr, jc] = 5
            else:
                if ir > jr:
                    ir, jr = jr, ir
                sss[ir, ic] = 2
                sss[jr, jc] = 3
            break

    return flow


SIGNATURE = '(i8,i8,i1[:,:],)'
if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC

    cc = CC('my_module')
    cc.export('solve', SIGNATURE)(solve)
    cc.compile()
    exit()

if os.name == 'posix':
    # noinspection PyUnresolvedReferences
    from my_module import solve
else:
    from numba import njit

    solve = njit(SIGNATURE, cache=True)(solve)
    print('compiled', file=sys.stderr)

n, m = map(int, input().split())
sss = np.ones((n + 2, m + 2), dtype=np.int8)
for i in range(n):
    sss[i + 1, 1:m + 1] = list(map('.#'.index, input()))

ans = solve(n, m, sss)
print(ans)
CHARS = '.#v^><'.__getitem__
for i in range(1, n + 1):
    print(''.join(map(CHARS, sss[i, 1:m + 1])))

Submission status: Accepted

Solution code:
import sys
from collections import deque
input = sys.stdin.readline


class Dinic:
    def __init__(self, v):
        self.vertice = v
        self.inf = int(1e9)
        self.graph = [[] for i in range(v)]
        self.level = [0]*v
        self.iter = [0]*v
    
    def add_edge(self, fr, to, cap, reverse=True):
        self.graph[fr].append([to, cap, len(self.graph[to])])
        cap2 = cap if reverse else 0
        self.graph[to].append([fr, cap2, len(self.graph[fr])-1])
    
    def bfs(self, s):
        self.level = [-1]*self.vertice
        que = deque([])
        self.level[s] = 0
        que.append(s)

        while que:
            v = que.popleft()
            for i in range(len(self.graph[v])):
                e = self.graph[v][i]
                if e[1] > 0 and self.level[e[0]] < 0:
                    self.level[e[0]] = self.level[v] + 1
                    que.append(e[0])
    
    def dfs(self, v, t, f):
        if v == t:
            return f
        for i in range(self.iter[v], len(self.graph[v])):
            self.iter[v] = i
            e = self.graph[v][i]
            if e[1] > 0 and self.level[v] < self.level[e[0]]:
                d = self.dfs(e[0], t, min(f, e[1]))
                if d > 0:
                    e[1] -= d
                    self.graph[e[0]][e[2]][1] += d
                    return d
        return 0
    
    def max_flow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.level[t] < 0:
                return flow
            self.iter = [0]*self.vertice

            f = self.dfs(s, t, self.inf)
            while f:
                flow += f
                f = self.dfs(s, t, self.inf)


def main():
    n, m = map(int, input().split())
    s = [[1 if x == "#" else 0 for x in input()[:-1]] for i in range(n)]
    
    flow = Dinic(n*m+2)
    start = 0
    end = n*m+1
    direction = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    for i in range(n):
        for j in range(m):
            if s[i][j] == 0:
                if (i+j)%2:
                    flow.add_edge(start, i*m+j+1, 1, reverse=False)
                    for x, y in direction:
                        ni, nj = x + i, y + j
                        if 0 <= ni < n and 0 <= nj < m and s[ni][nj] == 0:
                            flow.add_edge(i*m+j+1, ni*m+nj+1, 1, reverse=False)
                else:
                    flow.add_edge(i*m+j+1, end, 1, reverse=False)
    
    res = flow.max_flow(start, end)
    print(res)

    ans = [["#" if s[i][j] else "." for j in range(m)] for i in range(n)]

    flow_list = dict()
    for i, g in enumerate(flow.graph):
        for node in g:
            fr, to = i, node[0]
            if fr == start or to == end or to == start or fr == end:
                continue
            fr -= 1
            to -= 1
            xi, xj = divmod(fr, m)
            if (xi + xj) % 2 == 0:
                continue
            if node[1] == 0:
                if fr > to:
                    fr, to = to, fr
                flow_list[fr] = to
    
    for k, v in flow_list.items():
        if v-k == m:
            ans[k//m][k%m] = "v"
            ans[v//m][v%m] = "^"
        else:
            ans[k//m][k%m] = ">"
            ans[v//m][v%m] = "<"

    for a in ans:
        print("".join(a))

    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import os
import sys

import numpy as np


def solve(n, m, sss):
    BIT_SHIFT = 9
    BIT_MASK = (1 << BIT_SHIFT) - 1

    def combine_rc(r, c):
        return (r << BIT_SHIFT) | c

    def separate_rc(rc):
        return rc >> BIT_SHIFT, rc & BIT_MASK

    DINIC_LINKS = []
    DINIC_PAIRS = []

    def dinic_init(n):
        lst = [[0, 0, 0]]
        lst.clear()
        DINIC_LINKS.append([lst.copy() for _ in range(n)])
        DINIC_PAIRS.append(np.full(n, -1, dtype=np.int64))

    def dinic_add_link(ins, frm, to, cap):
        links = DINIC_LINKS[ins]
        links[frm].append([cap, to, len(links[to])])
        links[to].append([0, frm, len(links[frm]) - 1])

    def dinic_bfs(ins, n, s):
        links = DINIC_LINKS[ins]
        depth = np.full(n, -1, dtype=np.int64)
        depth[s] = 0
        deq = np.zeros(n + 5, dtype=np.int64)
        dl, dr = 0, 1
        deq[0] = s
        while dl < dr:
            v = deq[dl]
            dl += 1
            for link in links[v]:
                if link[0] > 0 and depth[link[1]] == -1:
                    depth[link[1]] = depth[v] + 1
                    deq[dr] = link[1]
                    dr += 1
        return depth

    def dinic_dfs(ins, depth, progress, s, t):
        links = DINIC_LINKS[ins]
        pairs = DINIC_PAIRS[ins]
        stack = [(s, 10 ** 18)]
        flow = 0
        while stack:
            v, f = stack.pop()
            if v == t:
                flow = f
                continue
            if flow == 0:
                i = progress[v]
                if i == len(links[v]):
                    continue
                progress[v] += 1
                stack.append((v, f))
                cap, to, rev = links[v][i]
                if cap == 0 or depth[v] >= depth[to]:
                    continue
                stack.append((to, min(f, cap)))
            else:
                i = progress[v] - 1
                link = links[v][i]
                link[0] -= flow
                links[link[1]][link[2]][0] += flow
                pairs[v] = link[1]
        return flow

    def dinic_maximum_flow(ins, n, s, t):
        flow = 0
        while True:
            depth = dinic_bfs(ins, n, s)
            if depth[t] == -1:
                return flow
            progress = np.zeros(n, dtype=np.int64)
            path_flow = dinic_dfs(ins, depth, progress, s, t)
            while path_flow != 0:
                flow += path_flow
                path_flow = dinic_dfs(ins, depth, progress, s, t)

    lefts = []
    rights = []
    rights_rev = {}

    for r in range(1, n + 1):
        for c in range(1, m + 1):
            if sss[r, c]:
                continue
            rc = combine_rc(r, c)
            if (r ^ c) & 1:
                lefts.append(rc)
            else:
                rights_rev[rc] = len(rights)
                rights.append(rc)
    l_size = len(lefts)
    r_size = len(rights)
    s = l_size + r_size
    t = s + 1
    total_size = t + 1

    dinic_init(total_size)

    MOVE = (-(1 << BIT_SHIFT), -1, 1, 1 << BIT_SHIFT)

    for i in range(l_size):
        rc = lefts[i]
        for drc in MOVE:
            nrc = rc + drc
            if nrc in rights_rev:
                dinic_add_link(0, i, rights_rev[nrc] + l_size, 1)
        dinic_add_link(0, s, i, 1)
    for j in range(r_size):
        dinic_add_link(0, j + l_size, t, 1)

    flow = dinic_maximum_flow(0, total_size, s, t)
    pairs = DINIC_PAIRS[0]

    for i in range(l_size):
        j = pairs[i]
        if j == -1:
            continue
        ir, ic = separate_rc(lefts[i])
        jr, jc = separate_rc(rights[j - l_size])
        if ir == jr:
            if ic > jc:
                ic, jc = jc, ic
            sss[ir, ic] = 4
            sss[jr, jc] = 5
        else:
            if ir > jr:
                ir, jr = jr, ir
            sss[ir, ic] = 2
            sss[jr, jc] = 3

    return flow


SIGNATURE = '(i8,i8,i1[:,:],)'
if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC

    cc = CC('my_module')
    cc.export('solve', SIGNATURE)(solve)
    cc.compile()
    exit()

if os.name == 'posix':
    # noinspection PyUnresolvedReferences
    from my_module import solve
else:
    from numba import njit

    solve = njit(SIGNATURE, cache=True)(solve)
    print('compiled', file=sys.stderr)

n, m = map(int, input().split())
sss = np.ones((n + 2, m + 2), dtype=np.int8)
for i in range(n):
    sss[i + 1, 1:m + 1] = list(map('.#'.index, input()))

ans = solve(n, m, sss)
print(ans)
CHARS = '.#v^><'.__getitem__
for i in range(1, n + 1):
    print(''.join(map(CHARS, sss[i, 1:m + 1])))