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:
from collections import deque

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

    def add_edge(self, fr, to, cap):
        #assert 0 <= fr < self.n
        #assert 0 <= to < self.n
        #assert 0 <= cap
        m = len(self.pos)
        self.pos.append((fr, len(self.graph[fr])))
        self.graph[fr].append([to, len(self.graph[to]), cap])
        self.graph[to].append([fr, len(self.graph[fr]) - 1, 0])
        return m

    def get_edge(self, idx):
        #assert 0 <= idx < len(self.pos)
        to, rev, cap = self.graph[self.pos[idx][0]][self.pos[idx][1]]
        rev_to, rev_rev, rev_cap = self.graph[to][rev]
        return rev_to, to, cap + rev_cap, rev_cap

    def edges(self):
        m = len(self.pos)
        for i in range(m):
            yield self.get_edge(i)

    def change_edge(self, idx, new_cap, new_flow):
        #assert 0 <= idx < len(self.pos)
        #assert 0 <= new_flow <= new_cap
        to, rev, cap = self.graph[self.pos[idx][0]][self.pos[idx][1]]
        self.graph[self.pos[idx][0]][self.pos[idx][1]][2] = new_cap - new_flow
        self.graph[to][rev][2] = new_flow

    def dfs(self, s, v, up):
        if v == s: return up
        res = 0
        lv = self.level[v]
        for i in range(self.iter[v], len(self.graph[v])):
            to, rev, cap = self.graph[v][i]
            if lv <= self.level[to] or self.graph[to][rev][2] == 0: continue
            d = self.dfs(s, to, min(up - res, self.graph[to][rev][2]))
            if d <= 0: continue
            self.graph[v][i][2] += d
            self.graph[to][rev][2] -= d
            res += d
            if res == up: break
            self.iter[v] += 1
        return res

    def max_flow(self, s, t):
        return self.max_flow_with_limit(s, t, 2**63 - 1)

    def max_flow_with_limit(self, s, t, limit):
        #assert 0 <= s < self.n
        #assert 0 <= t < self.n
        flow = 0
        while flow < limit:
            self.level = [-1] * self.n
            self.level[s] = 0
            queue = deque()
            queue.append(s)
            while queue:
                v = queue.popleft()
                for to, rev, cap in self.graph[v]:
                    if cap == 0 or self.level[to] >= 0: continue
                    self.level[to] = self.level[v] + 1
                    if to == t: break
                    queue.append(to)
            if self.level[t] == -1: break
            self.iter = [0] * self.n
            while flow < limit:
                f = self.dfs(s, t, limit - flow)
                if not f: break
                flow += f
        return flow

    def min_cut(self, s):
        visited = [0] * self.n
        queue = deque()
        queue.append(s)
        while queue:
            p = queue.popleft()
            visited[p] = True
            for to, rev, cap in self.graph[p]:
                if cap and not visited[to]:
                    visited[to] = True
                    queue.append(to)
        return visited

import sys
input = sys.stdin.readline

N, M = map(int, input().split())
S = [list(input().rstrip()) for _ in range(N)]

mf = MaxFlow(N * M + 2)

for i in range(N):
    for j in range(M):
        if S[i][j] == '#':
            continue
        if (i + j) % 2 == 0:
            mf.add_edge(N * M, i * M + j, 1)
            for di, dj in (1, 0), (0, 1), (-1, 0), (0, -1):
                ni = i + di
                nj = j + dj
                if 0 <= ni < N and 0 <= nj < M and S[ni][nj] == '.':
                    mf.add_edge(i * M + j, ni * M + nj, 1)
        else:
            mf.add_edge(i * M + j, N * M + 1, 1)

print(mf.max_flow(M * N, M * N + 1))

for fr, to, cap, flow in mf.edges():
    if flow == 0 or fr == N * M or to == N * M + 1:
        continue
    si, sj = divmod(fr, M)
    ti, tj = divmod(to, M)
    if si < ti:
        S[si][sj] = 'v'
        S[ti][tj] = '^'
    elif sj < tj:
        S[si][sj] = '>'
        S[ti][tj] = '<'
    elif si > ti:
        S[si][sj] = '^'
        S[ti][tj] = 'v'
    else:
        S[si][sj] = '<'
        S[ti][tj] = '>'

for s in S:
    print(''.join(s))

Submission status: Accepted

Solution code:
code = r"""
# distutils: language=c++
# distutils: include_dirs=[/home/contestant/.local/lib/python3.8/site-packages/numpy/core/include,/opt/atcoder-stl]
# cython: boundscheck=False
# cython: wraparound=False
from libcpp cimport bool
from libcpp.string cimport string
from libcpp.vector cimport vector

cdef extern from "<atcoder/dsu>" namespace "atcoder":
    cpdef cppclass dsu:
        dsu(int n)
        int merge(int a, int b)
        bool same(int a, int b)
        int leader(int a)
        int size(int a)
        vector[vector[int]] groups()
cdef class dsu_:
    cdef dsu *ptr
    def __cinit__(self, int n): self.ptr = new dsu(n)
    def __dealloc__(self): del self.ptr
    cpdef int merge(self,a,b): return self.ptr.merge(a,b)
    cpdef bool same(self,a,b): return self.ptr.same(a,b)
    cpdef int leader(self,a): return self.ptr.leader(a)
    cpdef int size(self,a): return self.ptr.size(a)
    cpdef vector[vector[int]] groups(self): return self.ptr.groups()

cdef extern from "<atcoder/fenwicktree>" namespace "atcoder":
    cdef cppclass fenwick_tree[T]:
        fenwick_tree()
        fenwick_tree(int n)
        void add(int p, T x)
        T sum(int l, int r)
cdef class fenwick_tree_:
    cdef fenwick_tree[int] *ptr
    def __cinit__(self, int n): self.ptr = new fenwick_tree[int](n)  
    def __dealloc__(self): del self.ptr
    cpdef void add(self, int p, long x): self.ptr.add(p, x)
    cpdef long sum(self, int l, int r): return self.ptr.sum(l, r)

cdef extern from "<atcoder/string>" namespace "atcoder":
    vector[int] suffix_array(vector[int] s, int upper)
    vector[int] suffix_array(vector[long] s)
    vector[int] suffix_array(string s)
    vector[int] lcp_array(vector[long] s, vector[int] sa)
    vector[int] lcp_array(string s, vector[int] sa)
    vector[int] z_algorithm(vector[long] s)
    vector[int] z_algorithm(string s)
cpdef vector[int] suffix_array_u(vector[int] s, int upper): return suffix_array(s, upper)
cpdef vector[int] suffix_array_l(vector[long] s): return suffix_array(s)
cpdef vector[int] suffix_array_s(string s): return suffix_array(s)
cpdef vector[int] lcp_array_l(vector[long] s, vector[int] sa): return lcp_array(s, sa)
cpdef vector[int] lcp_array_s(string s, vector[int] sa): return lcp_array(s, sa)
cpdef vector[int] z_algorithm_l(vector[long] s): return z_algorithm(s)
cpdef vector[int] z_algorithm_s(string s): return z_algorithm(s)

cdef extern from "<atcoder/maxflow>" namespace "atcoder":
    cdef cppclass mf_graph[Cap]:
        mf_graph(int n)
        int add_edge(int frm, int to, Cap cap)
        Cap flow(int s, int t)
        Cap flow(int s, int t, Cap flow_limit)
        vector[bool] min_cut(int s)
        cppclass edge:
            int frm "from", to
            Cap cap, flow
            edge(edge &e)
        edge get_edge(int i)
        vector[edge] edges()
        void change_edge(int i, Cap new_cap, Cap new_flow)
cdef class mf_graph_:
    cdef mf_graph[int] *ptr
    def __cinit__(self, int n): self.ptr = new mf_graph[int](n)  
    def __dealloc__(self): del self.ptr
    cpdef int add_edge(self, int frm, int to, int cap): return self.ptr.add_edge(frm, to, cap)
    cpdef int flow(self, int s, int t): return self.ptr.flow(s, t)
    cpdef int flow_limit(self, int s, int t, int flow_limit): return self.ptr.flow(s, t, flow_limit)
    cpdef vector[bool] min_cut(self, int s): return self.ptr.min_cut(s)
    cpdef vector[int] get_edge(self, int i):
        cdef mf_graph[int].edge *e = new mf_graph[int].edge(self.ptr.get_edge(i))
        cdef vector[int] *ret_e = new vector[int]()        
        ret_e.push_back(e.frm)
        ret_e.push_back(e.to)
        ret_e.push_back(e.cap)
        ret_e.push_back(e.flow)
        return ret_e[0]
    cpdef vector[vector[int]] edges(self):
        cdef vector[mf_graph[int].edge] es = self.ptr.edges()
        cdef vector[vector[int]] *ret_es = new vector[vector[int]](es.size())
        for i in range(es.size()):
            ret_es.at(i).push_back(es.at(i).frm)
            ret_es.at(i).push_back(es.at(i).to)
            ret_es.at(i).push_back(es.at(i).cap)
            ret_es.at(i).push_back(es.at(i).flow)
        return ret_es[0]
    cpdef void change_edge(self, int i, int new_cap, int new_flow): self.ptr.change_edge(i, new_cap, new_flow)
"""

import os
try:
    from atcoder import *
except:
    open('atcoder.pyx', 'w').write(code)
    os.system('cythonize -i -3 -b atcoder.pyx')

N,M = list(map(int,input().split()))
S = [input() for i in range(N)]
mg = mf_graph_(N*M + 2)
s = N*M
t = N*M + 1
for i in range(N):
    for j in range(M):
        if S[i][j]=="#":
            continue
        v = i*M + j
        if (i+j)%2==0:
            mg.add_edge(s,v,1)
        else:
            mg.add_edge(v,t,1)
for i in range(N):
    for j in range(M):
        if (i+j)%2==1 or S[i][j]=="#":
            continue
        v0 = i*M + j
        if i>0:
            if S[i-1][j]==".":
                v1 = (i-1)*M + j
                mg.add_edge(v0,v1,1)
        if j>0:
            if S[i][j-1]==".":
                v1 = i*M + j-1
                mg.add_edge(v0,v1,1)
        if i<N-1:
            if S[i+1][j]==".":
                v1 = (i+1)*M + j
                mg.add_edge(v0,v1,1)
        if j<M-1:
            if S[i][j+1]==".":
                v1 = i*M + j+1
                mg.add_edge(v0,v1,1)
print(mg.flow(s, t))
edges = mg.edges()
T = [list(__) for __ in S]
for e in edges:
    if e[0]==s or e[1]==t or e[3]==0:
        continue
    i0 = e[0]//M
    j0 = e[0]%M
    i1 = e[1]//M
    j1 = e[1]%M
    if i0 == i1+1:
        T[i1][j1] = 'v'
        T[i0][j0] = '^'
    elif j0 == j1+1:
        T[i1][j1] = '>'
        T[i0][j0] = '<'
    elif i0 == i1-1:
        T[i1][j1] = '^'
        T[i0][j0] = 'v'
    else:
        T[i1][j1] = '<'
        T[i0][j0] = '>'
for __ in T:
    print("".join(__))

Submission status: Accepted

Solution code:
# Dinic's algorithm
from collections import deque
class Dinic:
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]

    def add_edge(self, fr, to, cap):
        forward = [to, cap, None]
        forward[2] = backward = [fr, 0, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

    def add_multi_edge(self, v1, v2, cap1, cap2):
        edge1 = [v2, cap1, None]
        edge1[2] = edge2 = [v1, cap2, edge1]
        self.G[v1].append(edge1)
        self.G[v2].append(edge2)

    def bfs(self, s, t):
        self.level = level = [None]*self.N
        deq = deque([s])
        level[s] = 0
        G = self.G
        while deq:
            v = deq.popleft()
            lv = level[v] + 1
            for w, cap, _ in G[v]:
                if cap and level[w] is None:
                    level[w] = lv
                    deq.append(w)
        return level[t] is not None

    def dfs(self, v, t, f):
        if v == t:
            return f
        level = self.level
        for e in self.it[v]:
            w, cap, rev = e
            if cap and level[v] < level[w]:
                d = self.dfs(w, t, min(f, cap))
                if d:
                    e[1] -= d
                    rev[1] += d
                    return d
        return 0

    def flow(self, s, t):
        flow = 0
        INF = 10**9 + 7
        G = self.G
        while self.bfs(s, t):
            *self.it, = map(iter, self.G)
            f = INF
            while f:
                f = self.dfs(s, t, INF)
                flow += f
        return flow

N,M = map(int,input().split())
S = [input() for i in range(N)]

G = Dinic(N*M+2)

for i in range(N):
    for j in range(M):
        node = i*M + j + 1
        if (i+j)%2 and S[i][j]==".":
            G.add_edge(0,node,1)
            if i!=0 and S[i-1][j]==".":
                G.add_edge(node,node-M,1)
            if i!=N-1 and S[i+1][j]==".":
                G.add_edge(node,node+M,1)
            if j!=0 and S[i][j-1]==".":
                G.add_edge(node,node-1,1)
            if j!=M-1 and S[i][j+1]==".":
                G.add_edge(node,node+1,1)
        else:
            G.add_edge(node,N*M+1,1)

ans = G.flow(0,N*M+1)
print(ans)

res = [[S[i][j] for j in range(M)] for i in range(N)]

for i in range(N):
    for j in range(M):
        node = i*M + j + 1
        if (i+j)%2 and S[i][j]==".":
            for w,cap,rev in G.G[node]:
                if cap==0 and w!=0 and w!=N*M+1:
                    ni,nj = (w-1)//M,(w-1)%M
                    if ni==i-1:
                        res[ni][nj] = "v"
                        res[i][j] = "^"
                    elif ni==i+1:
                        res[i][j] = "v"
                        res[ni][nj] = "^"
                    elif nj==j-1:
                        res[ni][nj] = ">"
                        res[i][j] = "<"
                    else:
                        res[i][j] = ">"
                        res[ni][nj] = "<"

for i in range(N):
    print("".join(res[i]))

Submission status: Accepted

Solution code:
from collections import deque
class maxflow():
    def __init__(self, n):
        self.pos = []
        self.g = [[] for _ in range(n)]
        self.n = n
        self.cap = {}
   
    def add_edge(self, v, w, cap):
        e1 = [w, 0, cap]
        e2 = [v, e1, 0]
        e1[1] = e2
        self.g[v].append(e1)
        self.g[w].append(e2)
        self.cap[(v, w)] = [cap, e1]
    
    def bfs(self, s, t):
        L = [-1] * self.n
        L[s] = 0
        Q = deque([s])
        while Q:
            v = Q.popleft()
            for w, _, cap in self.g[v]:
                if cap == 0 or L[w] >= 0: continue
                L[w] = L[v] + 1
                if w == t:
                    self.L = L
                    return
                Q.append(w)
        self.L = L

    def dfs(self, s, v, up):
        if v == s: return up
        res = 0
        lv = self.L[v]
        for i in range(self.it[v], len(self.g[v])):
            w, rev, cap = self.g[v][i]
            if lv > self.L[w] and rev[2] > 0:
                d = self.dfs(s, w, min(up - res, rev[2]))
                if d > 0: 
                    self.g[v][i][2] += d
                    rev[2] -= d
                    res += d
                    if res == up: break
            self.it[v] += 1
        return res
    
    def flow(self, s, t, flow_limit = -1):
        if flow_limit < 0: flow_limit = 10 ** 100
        
        flow = 0
        while flow < flow_limit:
            self.bfs(s, t)
            if self.L[t] == -1: break
            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, self.g
    
    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
    
    def flow_all(self):
        L = []
        for a in self.cap:
            v, w = a
            c, e = self.cap[a]
            L.append((v, w, c - e[2]))
        return L


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, g = mf.flow(s, t)
for v, w, f in mf.flow_all():
    if max(v, w) >= N * M or not f: continue
    vi, vj = v // M, v % M
    wi, wj = w // M, w % M
    if v > w:
        vi, vj, wi, wj = wi, wj, vi, vj
    if vi == wi:
        A[vi][vj] = ">"
        A[wi][wj] = "<"
    else:
        A[vi][vj] = "v"
        A[wi][wj] = "^"

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

Submission status: Accepted

Solution code:
from collections import deque
class maxflow():
    def __init__(self, n):
        self.pos = []
        self.g = [[] for _ in range(n)]
        self.n = n
    
    def add_edge(self, v, w, cap):
        e1 = [w, 0, cap]
        e2 = [v, e1, 0]
        e1[1] = e2
        self.g[v].append(e1)
        self.g[w].append(e2)
    
    def bfs(self, s, t):
        L = [-1] * self.n
        L[s] = 0
        Q = deque([s])
        while Q:
            v = Q.popleft()
            for w, _, cap in self.g[v]:
                if cap == 0 or L[w] >= 0: continue
                L[w] = L[v] + 1
                if w == t:
                    self.L = L
                    return
                Q.append(w)
        self.L = L

    def dfs(self, s, v, up):
        if v == s: return up
        res = 0
        lv = self.L[v]
        for i in range(self.it[v], len(self.g[v])):
            w, rev, cap = self.g[v][i]
            if lv > self.L[w] and rev[2] > 0:
                d = self.dfs(s, w, min(up - res, rev[2]))
                if d > 0: 
                    self.g[v][i][2] += d
                    rev[2] -= d

                    ###
                    # return d
                    ###

                    res += d
                    if res == up: break
            self.it[v] += 1
        return res
    
    def flow(self, s, t, flow_limit = -1):
        if flow_limit < 0: flow_limit = 10 ** 100
        
        flow = 0
        while flow < flow_limit:
            self.bfs(s, t)
            if self.L[t] == -1: break
            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, self.g
    
    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, g = mf.flow(s, t)
# print("fl =", fl, g)
for v, gv in enumerate(g):
    if v >= N * M: continue
    for w, _, ca in gv:
        if w >= N * M: continue
        # if w > v: v, w = w, v
        vi, vj = v // M, v % M
        wi, wj = w // M, w % M
        if (vi ^ vj) % 2 == 0 and ca == 0:
            if v > w:
                vi, vj, wi, wj = wi, wj, vi, vj
            # print("v, w, ca =", v, w, ca)
            if vi == wi:
                A[vi][vj] = ">"
                A[wi][wj] = "<"
            else:
                A[vi][vj] = "v"
                A[wi][wj] = "^"

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

Submission status: Accepted

Solution code:
from collections import deque
class maxflow():
    def __init__(self, n):
        self.pos = []
        self.g = [[] for _ in range(n)]
        self.n = n
    
    def add_edge(self, v, w, cap):
        e1 = [w, 0, cap]
        e2 = [v, e1, 0]
        e1[1] = e2
        self.g[v].append(e1)
        self.g[w].append(e2)
    
    def bfs(self, s, t):
        L = [-1] * self.n
        L[s] = 0
        Q = deque([s])
        while Q:
            v = Q.popleft()
            for w, _, cap in self.g[v]:
                if cap == 0 or L[w] >= 0: continue
                L[w] = L[v] + 1
                if w == t:
                    self.L = L
                    return
                Q.append(w)
        self.L = L

    def dfs(self, s, v, up):
        if v == s: return up
        res = 0
        lv = self.L[v]
        for i in range(self.it[v], len(self.g[v])):
            w, rev, cap = self.g[v][i]
            if lv > self.L[w] and rev[2] > 0:
                d = self.dfs(s, w, min(up - res, rev[2]))
                if d > 0: 
                    self.g[v][i][2] += d
                    rev[2] -= d

                    ###
                    return d
                    ###

                    res += d
                    if res == up: break
            self.it[v] += 1
        return res
    
    def flow(self, s, t, flow_limit = -1):
        if flow_limit < 0: flow_limit = 10 ** 100
        
        flow = 0
        while flow < flow_limit:
            self.bfs(s, t)
            if self.L[t] == -1: break
            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, self.g
    
    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, g = mf.flow(s, t)
# print("fl =", fl, g)
for v, gv in enumerate(g):
    if v >= N * M: continue
    for w, _, ca in gv:
        if w >= N * M: continue
        # if w > v: v, w = w, v
        vi, vj = v // M, v % M
        wi, wj = w // M, w % M
        if (vi ^ vj) % 2 == 0 and ca == 0:
            if v > w:
                vi, vj, wi, wj = wi, wj, vi, vj
            # print("v, w, ca =", v, w, ca)
            if vi == wi:
                A[vi][vj] = ">"
                A[wi][wj] = "<"
            else:
                A[vi][vj] = "v"
                A[wi][wj] = "^"

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

Submission status: Accepted

Solution code:
class maxflow():
    def __init__(self, n):
        self.pos = []
        self.g = [[] for _ in range(n)]
        self.n = n
    
    def add_edge(self, v, w, cap):
        e1 = [w, 0, cap]
        e2 = [v, e1, 0]
        e1[1] = e2
        self.g[v].append(e1)
        self.g[w].append(e2)
    
    def bfs(self, s, t):
        L = [-1] * self.n
        L[s] = 0
        Q = [s]
        while Q:
            v = Q.pop()
            for w, _, cap in self.g[v]:
                if cap == 0 or L[w] >= 0: continue
                L[w] = L[v] + 1
                if w == t:
                    self.L = L
                    return
                Q.append(w)
        self.L = L

    def dfs(self, s, v, up):
        if v == s: return up
        res = 0
        lv = self.L[v]
        for i in range(self.it[v], len(self.g[v])):
            w, rev, cap = self.g[v][i]
            if lv > self.L[w] and rev[2] > 0:
                d = self.dfs(s, w, min(up - res, rev[2]))
                if d > 0: 
                    self.g[v][i][2] += d
                    rev[2] -= d

                    ###
                    return d
                    ###

                    res += d
                    if res == up: break
            self.it[v] += 1
        return res
    
    def flow(self, s, t, flow_limit = -1):
        if flow_limit < 0: flow_limit = 10 ** 100
        
        flow = 0
        while flow < flow_limit:
            self.bfs(s, t)
            if self.L[t] == -1: break
            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, self.g
    
    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, g = mf.flow(s, t)
# print("fl =", fl, g)
for v, gv in enumerate(g):
    if v >= N * M: continue
    for w, _, ca in gv:
        if w >= N * M: continue
        # if w > v: v, w = w, v
        vi, vj = v // M, v % M
        wi, wj = w // M, w % M
        if (vi ^ vj) % 2 == 0 and ca == 0:
            if v > w:
                vi, vj, wi, wj = wi, wj, vi, vj
            # print("v, w, ca =", v, w, ca)
            if vi == wi:
                A[vi][vj] = ">"
                A[wi][wj] = "<"
            else:
                A[vi][vj] = "v"
                A[wi][wj] = "^"

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