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.#
^><

Solution (Python):

from collections import deque
from typing import List

Edge = List[int]  


class Dinic:
    """Dinic class to solve max-find_max_flow / min-cut problems.
    References:
        https://ikatakos.com/pot/programming_algorithm/graph_theory/maximum_flow
        http://vartkw.hatenablog.com/entry/2016/12/02/002703
    """

    __slots__ = ["_size", "graph", "_depth", "_progress"]

    def __init__(self, size: int) -> None:
        self._size = size
        self.graph: List[List[Edge]] = [[] for _ in range(size)]
        self._depth = [-1] * size
        self._progress = [0] * size

    def add(self, from_: int, to: int, cap: int = 1, is_directed: bool = True) -> None:
        """Add an edge."""
        self.graph[from_].append([to, cap, len(self.graph[to])])
        self.graph[to].append([from_, 0, len(self.graph[from_]) - 1])
        if not is_directed:
            self.add(to, from_, cap)

    def find_max_flow(self, source: int, target: int, inf: int = 1 << 30) -> int:
        """Find the max-find_max_flow from source to target in O(N^{2}M)."""
        flow = 0
        while True:
            self._bfs(source)
            if self._depth[target] < 0:
                break
            self._progress = [0] * self._size
            cur_flow = self._dfs(source, target, inf)
            while cur_flow:
                flow += cur_flow
                cur_flow = self._dfs(source, target, inf)
        return flow

    def _bfs(self, source: int) -> None:
        depth = [-1] * self._size
        depth[source] = 0
        queue = deque([source])
        while queue:
            v = queue.popleft()
            for to, cap, _ in self.graph[v]:
                if cap > 0 > depth[to]:
                    depth[to] = depth[v] + 1
                    queue.append(to)
        self._depth = depth

    def _dfs(self, v: int, target: int, flow: int) -> int:
        if v == target:
            return flow
        v_edges = self.graph[v]
        for i in range(self._progress[v], len(v_edges)):
            self._progress[v] = i
            to, cap, rev = v_edges[i]
            if not cap or self._depth[v] >= self._depth[to]:
                continue
            d = self._dfs(to, target, min(flow, cap))
            if not d:
                continue
            v_edges[i][1] -= d
            self.graph[to][rev][1] += d
            return d
        return 0


def practice2_d():
    
    N, M = map(int, input().split())
    S = [list(input()) for _ in range(N)]
    dc = Dinic(N * M + 2)

    s = N * M
    t = N * M + 1
    for i in range(N):
        for j in range(M):
            if (i + j) % 2 == 0:
                dc.add(s, M * i + j)
            else:
                dc.add(M * i + j, t)

    for i in range(N):
        for j in range(M):
            if j + 1 < M and S[i][j] == "." and S[i][j + 1] == ".":
                u, v = M * i + j, M * i + j + 1
                if (i + j) % 2:
                    u, v = v, u
                dc.add(u, v)
            if i + 1 < N and S[i][j] == "." and S[i + 1][j] == ".":
                u, v = M * i + j, M * (i + 1) + j
                if (i + j) % 2:
                    u, v = v, u
                dc.add(u, v)

    print(dc.find_max_flow(s, t))
    for u in range(N * M + 2):
        for v, cap, _ in dc.graph[u]:
            ui, uj = divmod(u, M)
            vi, vj = divmod(v, M)
            if (
                not (ui + uj) % 2
                and not cap
                and u != s
                and u != t
                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 res in S:
        print("".join(res))


if __name__ == "__main__":
    practice2_d()

Solution (Python):
class maxflow():
    def __init__(self, n):
        self.pos = []
        self.g = [[] for _ in range(n)]
        self.n = n
    
    def add_edge(self, frm, to, cap):
        self.pos.append((frm, len(self.g[frm])))
        self.g[frm].append([to, len(self.g[to]), cap])
        self.g[to].append([frm, len(self.g[frm]) - 1, 0])
        return len(self.pos) - 1
    
    def get_edge(self, i):
        e = self.g[self.pos[i][0]][self.pos[i][1]]
        re = self.g[e[0]][e[1]]
        return (self.pos[i][0], e[0], e[2] + re[2], re[2])
    
    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.g[self.pos[i][0]][self.pos[i][1]]
        re = self.g[e[0]][e[1]]
        e[2] = new_cap - new_flow
        re[2] = new_flow
    
    def flow(self, s, t, flow_limit = -1):
        if flow_limit < 0: flow_limit = 10 ** 100
        it = [0] * self.n
        def bfs():
            L = [-1] * self.n
            L[s] = 0
            Q = [s]
            while Q:
                v = Q.pop()
                for e in self.g[v]:
                    if e[2] == 0 or L[e[0]] >= 0: continue
                    L[e[0]] = L[v] + 1
                    if e[0] == t: return L
                    Q.append(e[0])
            return L
        
        def dfs(v, up):
            if v == s: return up
            res = 0
            lv = L[v]
            for i in range(it[v], len(self.g[v])):
                e = self.g[v][i]
                if lv <= L[e[0]] or self.g[e[0]][e[1]][2] == 0: continue
                
                d = dfs(e[0], min(up - res, self.g[e[0]][e[1]][2]))
                if d <= 0: continue
                self.g[v][i][2] += d
                self.g[e[0]][e[1]][2] -= d
                res += d
                if res == up: break
            return res
        
        flow = 0
        while flow < flow_limit:
            L = bfs()
            if L[t] == -1: break
            it = [0] * self.n
            while flow < flow_limit:
                f = dfs(t, flow_limit - flow)
                if not f: break
                flow += f
        return flow
    
    def min_cut(self, s):
        visited = [0] * self.n
        Q = [s]
        while Q:
            p = Q.pop()
            visited[p] = 1
            for e in self.g[p]:
                if e[2] and visited[e[0]] == 0:
                    visited[e[0]] = 1
                    Q.append(e[0])
        return visited

N, M = map(int, input().split())
mf = maxflow(N * M + 2)
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:
                mf.add_edge(s, i * M + j, 1)
            else:
                mf.add_edge(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:
                mf.add_edge(a, b, 1)
            else:
                mf.add_edge(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:
                mf.add_edge(a, b, 1)
            else:
                mf.add_edge(b, a, 1)

fl = mf.flow(s, t)






for s, t, _, f in mf.edges():
    if f and max(s, t) < N * M:
        if s > t: s, t = t, s
        i, j = s // M, s % M
        ii, jj = t // M, t % M
        if i == ii:
            A[i][j] = ">"
            A[ii][jj] = "<"
        else:
            A[i][j] = "v"
            A[ii][jj] = "^"

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

Solution (Python):
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 = [np.empty(0, dtype=np.int64)]
        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(np.array([to, cap, len(links[to])], np.int64))
        links[to].append(np.array([frm, 0, len(links[frm]) - 1], np.int64))

    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':
    
    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('.

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

Solution (Python):



from collections import deque


class mf_graph:

    

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

    def add_edge(self, v, w, cap):
        self.graph[v].append(w)
        self.graph[w].append(v)
        self.capacities[v][w] = cap
        self.capacities[w][v] = 0

    def bfs(self, s, t):
        self.level = [-1] * self.N
        q = deque([s])
        self.level[s] = 0
        while q:
            v = q.popleft()
            if v == t:
                return True
            lw = self.level[v]+1
            for w, cap in self.capacities[v].items():
                if cap and self.level[w] == -1:
                    self.level[w] = lw
                    q.append(w)
        return self.level[t] != -1

    def dfs(self, s, t, up):
        st = [t]
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    v = self.graph[w][self.it[w]]
                    flow = min(flow, self.capacities[v][w])
                for w in st:
                    v = self.graph[w][self.it[w]]
                    self.capacities[w][v] += flow
                    self.capacities[v][w] -= flow
                return flow
            lv = self.level[v]
            while self.it[v] < len(self.graph[v]):
                w = self.graph[v][self.it[v]]
                cap = self.capacities[w][v]
                if cap and self.level[w] != -1:
                    if lv > self.level[w]:
                        st.append(w)
                        break
                self.it[v] += 1
            if self.it[v] == len(self.graph[v]):
                st.pop()
                self.level[v] = self.N
        return 0

    def flow(self, s, t, flow_limit=float('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 not f:
                    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 w, cap in self.capacities[v].items():
                if cap and not visited[w]:
                    q.append(w)


N, M = map(int, input().split())
S = [list(input()) for i in range(N)]
dc = mf_graph(N*M+2)

s = N*M
t = N*M+1
for i in range(N):
    for j in range(M):
        if (i+j) % 2 == 0:
            dc.add_edge(s, M*i+j, 1)
        else:
            dc.add_edge(M*i+j, t, 1)

for i in range(N):
    for j in range(M):
        if j+1 < M and S[i][j] == "." and S[i][j+1] == ".":
            u, v = M*i+j, M*i+j+1
            if (i+j) % 2 == 1:
                u, v = v, u
            dc.add_edge(u, v, 1)
        if i+1 < N and S[i][j] == "." and S[i + 1][j] == ".":
            u, v = M*i+j, M*(i+1)+j
            if (i+j) % 2 == 1:
                u, v = v, u
            dc.add_edge(u, v, 1)

print(dc.flow(s, t))
for u in range(N*M+2):
    ui, uj = divmod(u, M)
    if (ui+uj) % 2 == 1:
        continue
    for v, cap in dc.capacities[u].items():
        if cap != 0:
            continue
        vi, vj = divmod(v, M)
        if u != s and u != t 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 res in S:
    print("".join(res))

Solution (Python):



from collections import deque


class mf_graph:

    __slots__ = ["N", "graph", "capacities", "level", "it"]

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

    def add_edge(self, v, w, cap):
        self.graph[v].append(w)
        self.graph[w].append(v)
        self.capacities[v][w] = cap
        self.capacities[w][v] = 0

    def bfs(self, s, t):
        self.level = [-1] * self.N
        q = deque([s])
        self.level[s] = 0
        while q:
            v = q.popleft()
            lw = self.level[v]+1
            for w, cap in self.capacities[v].items():
                if cap and self.level[w] == -1:
                    self.level[w] = lw
                    q.append(w)
        return self.level[t] != -1

    def dfs(self, s, t, up):
        st = [t]
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    v = self.graph[w][self.it[w]]
                    flow = min(flow, self.capacities[v][w])
                for w in st:
                    v = self.graph[w][self.it[w]]
                    self.capacities[w][v] += flow
                    self.capacities[v][w] -= flow
                return flow
            lv = self.level[v]
            while self.it[v] < len(self.graph[v]):
                w = self.graph[v][self.it[v]]
                cap = self.capacities[w][v]
                if cap and self.level[w] != -1:
                    if lv > self.level[w]:
                        st.append(w)
                        break
                self.it[v] += 1
            if self.it[v] == len(self.graph[v]):
                st.pop()
                self.level[v] = self.N
        return 0

    def flow(self, s, t, flow_limit=float('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 not f:
                    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 w, cap in self.capacities[v].items():
                if cap and not visited[w]:
                    q.append(w)


N, M = map(int, input().split())
S = [list(input()) for i in range(N)]
dc = mf_graph(N*M+2)

s = N*M
t = N*M+1
for i in range(N):
    for j in range(M):
        if (i+j) % 2 == 0:
            dc.add_edge(s, M*i+j, 1)
        else:
            dc.add_edge(M*i+j, t, 1)

for i in range(N):
    for j in range(M):
        if j+1 < M and S[i][j] == "." and S[i][j+1] == ".":
            u, v = M*i+j, M*i+j+1
            if (i+j) % 2 == 1:
                u, v = v, u
            dc.add_edge(u, v, 1)
        if i+1 < N and S[i][j] == "." and S[i + 1][j] == ".":
            u, v = M*i+j, M*(i+1)+j
            if (i+j) % 2 == 1:
                u, v = v, u
            dc.add_edge(u, v, 1)

print(dc.flow(s, t))
for u in range(N*M+2):
    ui, uj = divmod(u, M)
    if (ui+uj) % 2 == 1:
        continue
    for v, cap in dc.capacities[u].items():
        if cap != 0:
            continue
        vi, vj = divmod(v, M)
        if u != s and u != t 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 res in S:
    print("".join(res))

Solution (Python):



from collections import deque


class mf_graph:

    

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

    def add_edge(self, v, w, cap):
        self.graph[v].append(w)
        self.graph[w].append(v)
        self.capacities[v][w] = cap
        self.capacities[w][v] = 0

    def bfs(self, s, t):
        self.level = [-1] * self.N
        q = deque([s])
        self.level[s] = 0
        while q:
            v = q.popleft()
            lw = self.level[v]+1
            for w, cap in self.capacities[v].items():
                if cap and self.level[w] == -1:
                    self.level[w] = lw
                    q.append(w)
        return self.level[t] != -1

    def dfs(self, s, t, up):
        st = [t]
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    v = self.graph[w][self.it[w]]
                    flow = min(flow, self.capacities[v][w])
                for w in st:
                    v = self.graph[w][self.it[w]]
                    self.capacities[w][v] += flow
                    self.capacities[v][w] -= flow
                return flow
            lv = self.level[v]
            while self.it[v] < len(self.graph[v]):
                w = self.graph[v][self.it[v]]
                cap = self.capacities[w][v]
                if cap and self.level[w] != -1:
                    if lv > self.level[w]:
                        st.append(w)
                        break
                self.it[v] += 1
            if self.it[v] == len(self.graph[v]):
                st.pop()
                self.level[v] = self.N
        return 0

    def flow(self, s, t, flow_limit=float('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 not f:
                    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 w, cap in self.capacities[v].items():
                if cap and not visited[w]:
                    q.append(w)


N, M = map(int, input().split())
S = [list(input()) for i in range(N)]
dc = mf_graph(N*M+2)

s = N*M
t = N*M+1
for i in range(N):
    for j in range(M):
        if (i+j) % 2 == 0:
            dc.add_edge(s, M*i+j, 1)
        else:
            dc.add_edge(M*i+j, t, 1)

for i in range(N):
    for j in range(M):
        if j+1 < M and S[i][j] == "." and S[i][j+1] == ".":
            u, v = M*i+j, M*i+j+1
            if (i+j) % 2 == 1:
                u, v = v, u
            dc.add_edge(u, v, 1)
        if i+1 < N and S[i][j] == "." and S[i + 1][j] == ".":
            u, v = M*i+j, M*(i+1)+j
            if (i+j) % 2 == 1:
                u, v = v, u
            dc.add_edge(u, v, 1)

print(dc.flow(s, t))
for u in range(N*M+2):
    ui, uj = divmod(u, M)
    if (ui+uj) % 2 == 1:
        continue
    for v, cap in dc.capacities[u].items():
        if cap != 0:
            continue
        vi, vj = divmod(v, M)
        if u != s and u != t 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 res in S:
    print("".join(res))

Solution (Python):



from collections import deque


class mf_graph:

    

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

    def add_edge(self, v, w, cap):
        self.graph[v].append(w)
        self.graph[w].append(v)
        self.capacities[v][w] = cap
        self.capacities[w][v] = 0

    def bfs(self, s, t):
        self.level = [-1] * self.N
        q = deque([s])
        self.level[s] = 0
        while q:
            v = q.popleft()
            lw = self.level[v]+1
            for w, cap in self.capacities[v].items():
                if cap and self.level[w] == -1:
                    self.level[w] = lw
                    q.append(w)
        return self.level[t] != -1

    def dfs(self, s, t, up):
        st = [t]
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    v = self.graph[w][self.it[w]]
                    flow = min(flow, self.capacities[v][w])
                for w in st:
                    v = self.graph[w][self.it[w]]
                    self.capacities[w][v] += flow
                    self.capacities[v][w] -= flow
                return flow
            lv = self.level[v]
            while self.it[v] < len(self.graph[v]):
                w = self.graph[v][self.it[v]]
                cap = self.capacities[w][v]
                if cap and self.level[w] != -1:
                    if lv > self.level[w]:
                        st.append(w)
                        break
                self.it[v] += 1
            if self.it[v] == len(self.graph[v]):
                st.pop()
                self.level[v] = self.N
        return 0

    def flow(self, s, t, flow_limit=float('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 not f:
                    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 w, cap in self.capacities[v].items():
                if cap and not visited[w]:
                    q.append(w)


N, M = map(int, input().split())
S = [list(input()) for i in range(N)]
dc = mf_graph(N*M+2)

s = N*M
t = N*M+1
for i in range(N):
    for j in range(M):
        if (i+j) % 2 == 0:
            dc.add_edge(s, M*i+j, 1)
        else:
            dc.add_edge(M*i+j, t, 1)

for i in range(N):
    for j in range(M):
        if j+1 < M and S[i][j] == "." and S[i][j+1] == ".":
            u, v = M*i+j, M*i+j+1
            if (i+j) % 2 == 1:
                u, v = v, u
            dc.add_edge(u, v, 1)
        if i+1 < N and S[i][j] == "." and S[i + 1][j] == ".":
            u, v = M*i+j, M*(i+1)+j
            if (i+j) % 2 == 1:
                u, v = v, u
            dc.add_edge(u, v, 1)

print(dc.flow(s, t))
for u in range(N*M+2):
    ui, uj = divmod(u, M)
    if (ui+uj) % 2 == 1:
        continue
    for v, cap in dc.capacities[u].items():
        vi, vj = divmod(v, M)
        if cap == 0 and u != s and u != t 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 res in S:
    print("".join(res))