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:
# https://atcoder.jp/contests/practice2/submissions/16598132
# 上記のリンクの提出をいじらせてもらってます。

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 = deque([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):
    for v, cap in dc.capacities[u].items():
        ui, uj = divmod(u, M)
        vi, vj = divmod(v, M)
        if (ui+uj) % 2 == 0 and 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))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/practice2/submissions/16598132
# 上記のリンクの提出をいじらせてもらってます。

from collections import deque


class mf_graph:

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

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

    def add_edge(self, v, w, cap):
        edge = [w, cap, None]
        edge[2] = rev_edge = [v, 0, edge]
        self.graph[v].append(edge)
        self.graph[w].append(rev_edge)

    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.graph[v]:
                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 = deque([t])
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    e = self.graph[w][self.it[w]][2]
                    flow = min(flow, e[1])
                for w in st:
                    e = self.graph[w][self.it[w]]
                    e[1] += flow
                    e[2][1] -= flow
                return flow
            lv = self.level[v]
            while self.it[v] < len(self.graph[v]):
                w, _, e = self.graph[v][self.it[v]]
                cap = e[1]
                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.graph[v]:
                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):
    for v, cap, _ in dc.graph[u]:
        ui, uj = divmod(u, M)
        vi, vj = divmod(v, M)
        if (ui+uj) % 2 == 0 and 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))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/practice2/submissions/16598132
# 上記のリンクの提出をいじらせてもらってます。

from collections import deque


class mf_graph:

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

    def add_edge(self, v, w, cap):
        edge = [w, cap, None]
        edge[2] = rev_edge = [v, 0, edge]
        self.graph[v].append(edge)
        self.graph[w].append(rev_edge)

    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.graph[v]:
                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 = deque([t])
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    e = self.graph[w][self.it[w]][2]
                    flow = min(flow, e[1])
                for w in st:
                    e = self.graph[w][self.it[w]]
                    e[1] += flow
                    e[2][1] -= flow
                return flow
            lv = self.level[v]
            while self.it[v] < len(self.graph[v]):
                w, _, e = self.graph[v][self.it[v]]
                cap = e[1]
                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.graph[v]:
                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):
    for v, cap, _ in dc.graph[u]:
        ui, uj = divmod(u, M)
        vi, vj = divmod(v, M)
        if (ui+uj) % 2 == 0 and 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))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/practice2/submissions/16598132
# 上記のリンクの提出をいじらせてもらってます。

from collections import deque


class mf_graph:

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

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

    def add_edge(self, v, w, cap):
        edge = [w, cap, None]
        edge[2] = rev_edge = [v, 0, edge]
        self.graph[v].append(edge)
        self.graph[w].append(rev_edge)

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

    def dfs(self, s, t, up):
        st = deque([t])
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    e = self.graph[w][self.it[w]][2]
                    flow = min(flow, e[1])
                for w in st:
                    e = self.graph[w][self.it[w]]
                    e[1] += flow
                    e[2][1] -= flow
                return flow
            lv = self.level[v]
            while self.it[v] < len(self.graph[v]):
                w, _, e = self.graph[v][self.it[v]]
                cap = e[1]
                if cap and self.level[w] is not None:
                    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.graph[v]:
                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):
    for v, cap, _ in dc.graph[u]:
        ui, uj = divmod(u, M)
        vi, vj = divmod(v, M)
        if (ui+uj) % 2 == 0 and 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))

Submission status: Accepted

Solution code:
# Dinic's algorithm
#https://tjkendev.github.io/procon-library/python/max_flow/dinic.htmlより
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 = [list(input()) for i 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_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):
    for v, cap, _ in dc.G[u]:
        ui, uj = divmod(u, M)
        vi, vj = divmod(v, M)
        if (ui+uj) % 2 == 0 and 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))

Submission status: Accepted

Solution code:
from collections import deque

class mf_graph:
    def __init__(self,N):
        self.N = N
        self.graph = [[] for _ in range(N)]
    
    def add_edge(self,v,w,cap):
        edge = [w,cap,None]
        edge[2] = rev_edge = [v,0,edge]
        self.graph[v].append(edge)
        self.graph[w].append(rev_edge)
    
    def bfs(self,s,t):
        self.level = [None]*self.N
        q = deque([s])
        self.level[s] = 0
        while q:
            v = q.popleft()
            lw = self.level[v]+1
            for w,cap,_ in self.graph[v]:
                if cap and self.level[w] is None:
                    self.level[w] = lw
                    q.append(w)
        return self.level[t] is not None
    
    def dfs(self,s,t,up):
        st = deque([t])
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    e = self.graph[w][self.it[w]][2]
                    flow = min(flow,e[1])
                for w in st:
                    e = self.graph[w][self.it[w]]
                    e[1] += flow
                    e[2][1] -= flow
                return flow
            lv = self.level[v]
            while self.it[v] < len(self.graph[v]):
                w,_,e = self.graph[v][self.it[v]]
                cap = e[1]
                if cap and self.level[w] is not None:
                    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.graph[v]:
                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,10**9))
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 (ui+uj) % 2 == 0 and 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))

Submission status: Accepted

Solution code:
from collections import deque

class mf_graph:
    def __init__(self,N):
        self.N = N
        self.graph = [[] for _ in range(N)]
    
    def add_edge(self,v,w,cap):
        edge = [w,cap,None]
        edge[2] = rev_edge = [v,0,edge]
        self.graph[v].append(edge)
        self.graph[w].append(rev_edge)
    
    def bfs(self,s,t):
        self.level = [None]*self.N
        q = deque([s])
        self.level[s] = 0
        while q:
            v = q.popleft()
            lw = self.level[v]+1
            for w,cap,_ in self.graph[v]:
                if cap and self.level[w] is None:
                    self.level[w] = lw
                    q.append(w)
        return self.level[t] is not None
    
    def dfs(self,s,t,up):
        st = deque([t])
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    e = self.graph[w][self.it[w]][2]
                    flow = min(flow,e[1])
                for w in st:
                    e = self.graph[w][self.it[w]]
                    e[1] += flow
                    e[2][1] -= flow
                return flow
            lv = self.level[v]
            while self.it[v] < len(self.graph[v]):
                w,_,e = self.graph[v][self.it[v]]
                cap = e[1]
                if cap and self.level[w] is not None:
                    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.graph[v]:
                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):
    for v, cap, _ in dc.graph[u]:
        ui, uj = divmod(u, M)
        vi, vj = divmod(v, M)
        if (ui+uj) % 2 == 0 and 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))

Submission status: Accepted

Solution code:

"""
sから奇数へ，奇数から偶数へ，偶数からtへ
"""
def main():
    # Dinic's algorithm
    import collections
    class Dinic:
        def __init__(self, vnum):
            self.edge = [[] for i in range(vnum)]
            self.n = vnum
            self.inf = 10**10
        def add_edge(self, start, end, c):
            self.edge[start].append([end, c, len(self.edge[end])])
            self.edge[end].append([start, 0, len(self.edge[start])-1])
        def bfs(self, vst):
            dist = [-1]*self.n
            dist[vst] = 0
            Q = collections.deque([vst])
            while Q:
                nv = Q.popleft()
                for vt, c, r in self.edge[nv]:
                    if dist[vt] == -1 and c > 0:
                        dist[vt] = dist[nv] + 1
                        Q.append(vt)
            self.dist = dist
        def dfs(self, nv, en, nf):
            nextv = self.nextv
            if nv == en:
                return nf
            dist = self.dist
            ist = nextv[nv]
            for i, (vt, c, r) in enumerate(self.edge[nv][ist:], ist):
                if dist[nv] < dist[vt] and c > 0:
                    df = self.dfs(vt, en, min(nf, c))
                    if df > 0:
                        self.edge[nv][i][1] -= df
                        self.edge[vt][r][1] += df
                        return df
                nextv[nv] += 1
            return 0
        def get_max_flow(self, st, en):
            mf = 0
            while True:
                self.bfs(st)
                if self.dist[en] == -1:
                    break
                self.nextv = [0]*self.n
                while True:
                    fl = self.dfs(st, en, self.inf)
                    if fl > 0:
                        mf += fl
                    else:
                        break
            return mf
        
    mod=10**9+7
    N,M=map(int, input().split())
    S=[]
    for _ in range(N):
        s=list(input())
        S.append(s)
        
    din=Dinic(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=M*i + j
            if (i+j)%2==1:
                din.add_edge(s,v,1)
            else:
                din.add_edge(v,t,1)
                
    # 場内
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    
    for i in range(N):
        for j in range(M):
            if S[i][j]=="#":
                continue
            v=M*i + j
            if (i+j)%2==1:
                for k in range(4):
                    ni=i+dx[k]
                    nj=j+dy[k]
                    
                    if 0<=ni<N and 0<=nj<M:
                        if S[ni][nj]== "#":
                            continue
                        nv=M*ni + nj
                        din.add_edge(v,nv,1)
                        
    
    ans=din.get_max_flow(s,t)
    
    T=[s[:] for s in S]
            
    
    for v in range(N*M):
        i,j = divmod(v,M)
        if (i+j)%2==1:
            if S[i][j]==".":
                for nv,cap,_ in din.edge[v]:
                    if cap==0 and nv!=t and nv!=s:#使った辺を見る
                        ni,nj=divmod(nv,M)
                        # print(i,j," ",ni,nj)
                        if i==ni:
                            if j<nj:
                                T[i][j]=">"
                                T[ni][nj]="<"
                            else:
                                T[i][j]="<"
                                T[ni][nj]=">"
                        else:
                            if i<ni:
                                T[i][j]="v"
                                T[ni][nj]="^"
                            else:
                                T[i][j]="^"
                                T[ni][nj]="v"
                                
                                
    print(ans)
    for i in range(N):
        print(''.join(map(str, T[i])))
                                
                                
                                
            
    
                    
        
    
    

main()