One-Way Reform (Codeforces 723E)
Tags: Others, flows

There are n cities and m two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads.The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it.

Input: The first line contains a positive integer t (1 ≤ t ≤ 200) — the number of testsets in the input.Each of the testsets is given in the following way. The first line contains two integers n and m (1 ≤ n ≤ 200, 0 ≤ m ≤ n·(n - 1) / 2) — the number of cities and the number of roads in Berland. The next m lines contain the description of roads in Berland. Each line contains two integers u and v (1 ≤ u, v ≤ n) — the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities.It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200.Pay attention that for hacks, you can only use tests consisting of one testset, so t should be equal to one.

Output: For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it.In the next m lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. 


Sample Inputs:
2
5 5
2 1
4 5
2 3
1 3
3 5
7 2
3 7
4 2

Sample Outputs:
3
1 3
3 5
5 4
3 2
2 1
3
2 4
3 7

Solution (Python):
import sys
import collections
rlines = sys.stdin.readlines()
lines = (l.strip() for l in rlines)

def eucycle(n, m, adj):
    diredges = []
    us = list(adj.keys())
    for u in us:
        while adj[u]:
            v0 = u
            v1 = adj[v0].pop()
            adj[v1].remove(v0)
            diredges.append((v0, v1))
            while v1 != u:
                v0 = v1
                v1 = adj[v0].pop()
                adj[v1].remove(v0)
                diredges.append((v0, v1))
    return diredges

def solve(n, m, edges):
    adj = collections.defaultdict(set)
    diredges = []
    for (u, v) in edges:
        adj[u].add(v)
        adj[v].add(u)
    odds = set((u for u in adj if len(adj[u]) % 2 == 1))
    ans = n - len(odds)
    assert len(odds) % 2 == 0
    for o in odds:
        adj[n + 1].add(o)
        adj[o].add(n + 1)
    diredges = eucycle(n + 1, m, adj)
    return str(ans) + '\n' + '\n'.join((str(u) + ' ' + str(v) for (u, v) in diredges if u != n + 1 and v != n + 1))
t = int(next(lines))
for ti in range(t):
    (n, m) = [int(s) for s in next(lines).split()]
    edges = []
    for ei in range(m):
        (u, v) = [int(s) for s in next(lines).split()]
        edges.append((u, v))
    print(solve(n, m, edges))

Solution (Python):
from collections import defaultdict, Counter
T = int(input())
for _ in range(T):
    global lst
    (N, M) = map(int, input().split())
    visit = set()
    oddNodes = []
    directed = []
    G = defaultdict(list)
    oriG = defaultdict(list)
    Gra = [[0] * (N + 1) for _ in range(N + 1)]
    deg = [0] * (N + 1)
    for k in range(M):
        (u, v) = map(int, input().split())
        Gra[u][v] += 1
        Gra[v][u] += 1
        deg[u] += 1
        deg[v] += 1
    ans = 0
    for i in range(1, N + 1):
        if deg[i] % 2 == 0:
            ans += 1
        else:
            oddNodes.append(i)
    for i in range(0, len(oddNodes), 2):
        Gra[oddNodes[i]][oddNodes[i + 1]] += 1
        Gra[oddNodes[i + 1]][oddNodes[i]] += 1
        deg[oddNodes[i]] += 1
        deg[oddNodes[i + 1]] += 1

    def eulerPath(u):
        stk = [u]
        while stk:
            u = stk.pop()
            for i in range(1, N + 1):
                if Gra[u][i]:
                    Gra[u][i] -= 1
                    Gra[i][u] -= 1
                    directed.append((u, i))
                    stk.append(i)
                    break
    for i in range(1, N + 1):
        eulerPath(i)
    for i in range(0, len(oddNodes), 2):
        Gra[oddNodes[i]][oddNodes[i + 1]] += 1
        Gra[oddNodes[i + 1]][oddNodes[i]] += 1
    print(ans)
    for (u, v) in directed:
        if Gra[u][v] != 0:
            Gra[u][v] -= 1
            Gra[v][u] -= 1
        else:
            print(str(u) + ' ' + str(v))

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
ans = []
for _ in range(t):
    (n, m) = map(int, input().split())
    G = [set() for _ in range(n + 1)]
    for _ in range(m):
        (u, v) = map(int, input().split())
        G[u].add(v)
        G[v].add(u)
    ans0 = n
    ans1 = []
    for i in range(1, n + 1):
        if not len(G[i]) % 2:
            continue
        ans0 -= 2
        u = i
        while True:
            v = G[u].pop()
            ans1.append(' '.join(map(str, (u, v))))
            G[v].remove(u)
            if not len(G[v]) % 2:
                break
            u = v
    for i in range(1, n + 1):
        if not len(G[i]):
            continue
        u = i
        while True:
            v = G[u].pop()
            ans1.append(' '.join(map(str, (u, v))))
            G[v].remove(u)
            if v == i:
                break
            u = v
    ans.append(str(ans0))
    ans.append('\n'.join(ans1))
sys.stdout.write('\n'.join(ans))