Language: C++

from collections import deque

inf = 100000000000000000

def bfs(graph, s, t):
    V = len(graph)
    q = deque([(s, -1, -1)])
    prev = [(-1, -1)] * V
    visited = [False] * V
    while len(q) > 0:
        (v, p, e) = q.popleft()
        if visited[v]:
            continue
        visited[v] = True
        prev[v] = p, e
        for i in range(len(graph[v])):
            e = graph[v][i]
            if e['cap'] > 0:
                q.append((e['to'], v, i))
    if not visited[t]:
        return None
    else:
        return prev

def decrease_graph(graph, s, t, prev):
    ((p, e), c) = prev[t], t
    m = inf
    while p != -1:
        m = min(graph[p][e]['cap'], m)
        ((p, e), c) = prev[p], p
    ((p, e), c) = prev[t], t
    while p != -1:
        graph[p][e]['cap'] -= m
        graph[c][graph[p][e]['rev']]['cap'] += m
        ((p, e), c) = prev[p], p
    return m

def maximum_flow(graph, s, t):
    V = len(graph)
    g = [[] for _ in range(V)]
    for v in range(V):
        nei = graph[v]
        for (u, cap) in nei:
            g[v].append({'to' : u, 'from': v, 'cap': cap, 'rev': len(g[u])})
            g[u].append({'to' : v, 'from': u, 'cap':   0, 'rev': len(g[v]) - 1})
    
    sum = 0
    while True:
        prev = bfs(g, s, t)
        if prev == None:
            break
        sum += decrease_graph(g, s, t, prev)
    return sum

def gcd(x, y):
    while y > 0:
        x, y = y, x % y
    return x

def main():
    while True:
        m, n = map(int, input().split())
        if m == 0 and n == 0:
            return
        data = []
        while len(data) < n + m:
            data += list(map(int, input().split()))
        b = data[:m]
        r = data[m:]
        while len(r) < n:
            r += list(map(int, input().split()))
        
        g = [[] for i in range(n + m + 2)]
        for i in range(m):
            g[0].append((i + 1, 1))
        for i in range(n):
            g[m + i + 1].append((n + m + 1, 1))
        for i in range(m):
            for j in range(n):
                if gcd(b[i], r[j]) > 1:
                    g[i + 1].append((m + j + 1, 1))

        print(maximum_flow(g, 0, n + m + 1))
        
main()

Compiler output:
a.cc:18:18: warning: multi-character character constant [-Wmultichar]
   18 |             if e['cap'] > 0:
      |                  ^~~~~
a.cc:19:29: warning: multi-character character constant [-Wmultichar]
   19 |                 q.append((e['to'], v, i))
      |                             ^~~~
a.cc:29:29: warning: multi-character character constant [-Wmultichar]
   29 |         m = min(graph[p][e]['cap'], m)
      |                             ^~~~~
a.cc:33:21: warning: multi-character character constant [-Wmultichar]
   33 |         graph[p][e]['cap'] -= m
      |                     ^~~~~
a.cc:34:30: warning: multi-character character constant [-Wmultichar]
   34 |         graph[c][graph[p][e]['rev']]['cap'] += m
      |                              ^~~~~
a.cc:34:38: warning: multi-character character constant [-Wmultichar]
   34 |         graph[c][graph[p][e]['rev']]['cap'] += m
      |                                      ^~~~~
a.cc:44:26: warning: multi-character character constant [-Wmultichar]
   44 |             g[v].append({'to' : u, 'from': v, 'cap': cap, 'rev': len(g[u])})
      |                          ^~~~
a.cc:44:36: warning: multi-character character constant [-Wmultichar]
   44 |             g[v].append({'to' : u, 'from': v, 'cap': cap, 'rev': len(g[u])})
      |                                    ^~~~~~
a.cc:44:47: warning: multi-character character constant [-Wmultichar]
   44 |             g[v].append({'to' : u, 'from': v, 'cap': cap, 'rev': len(g[u])})
      |                                               ^~~~~
a.cc:44:59: warning: multi-character character constant [-Wmultichar]
   44 |             g[v].append({'to' : u, 'from': v, 'cap': cap, 'rev': len(g[u])})
      |                                                           ^~~~~
a.cc:45:26: warning: multi-character character constant [-Wmultichar]
   45 |             g[u].append({'to' : v, 'from': u, 'cap':   0, 'rev': len(g[v]) - 1})
      |                          ^~~~
a.cc:45:36: warning: multi-character character constant [-Wmultichar]
   45 |             g[u].append({'to' : v, 'from': u, 'cap':   0, 'rev': len(g[v]) - 1})
      |                                    ^~~~~~
a.cc:45:47: warning: multi-character character constant [-Wmultichar]
   45 |             g[u].append({'to' : v, 'from': u, 'cap':   0, 'rev': len(g[v]) - 1})
      |                                               ^~~~~
a.cc:45:59: warning: multi-character character constant [-Wmultichar]
   45 |             g[u].append({'to' : v, 'from': u, 'cap':   0, 'rev': len(g[v]) - 1})
      |                                                           ^~~~~
a.cc:1:1: error: 'from' does not name a type
    1 | from collections import deque
      | ^~~~
a.cc:44:76: error: expected unqualified-id before ')' token
   44 |             g[v].append({'to' : u, 'from': v, 'cap': cap, 'rev': len(g[u])})
      |                                                                            ^
a.cc:45:80: error: expected unqualified-id before ')' token
   45 |             g[u].append({'to' : v, 'from': u, 'cap':   0, 'rev': len(g[v]) - 1})
      |                                                                                ^