Under construction

Still being built. Things may change

Parity Constraint Maximum Flow

Time limit1sMemory limit1024 MB

Summary
Find a maximum s-t flow on a directed graph where each edge's integer flow must match a given parity (0 or 1), or report that none exists.
Level

Medium7 of 10

Topics
Graph, BFS, Math
Solved
No attempts yet

Problem

A flow network (G,c,s,t)(G, c, s, t) is defined as follows.

  • GG is a directed graph (V,E)(V, E).
  • c:E→R+c : E \rightarrow \mathbb{R}^+ is a function giving the capacity of each edge.
  • s,t∈Vs, t \in V are the source and the sink, respectively, and s≠ts \ne t.

An ss-tt flow ff on a flow network (G,c,s,t)(G, c, s, t) satisfies the following.

  • For every e∈Ee \in E, 0≤f(e)≤c(e)0 \le f(e) \le c(e).
  • For every v∈V−{s,t}v \in V-\{s, t\}, ∑(u,v)∈Ef(u,v)=∑(v,w)∈Ef(v,w)\sum_{(u,v) \in E} f(u,v) = \sum_{(v,w) \in E} f(v,w).

The value of the flow ff is ∣f∣=∑(s,v)∈Ef(s,v)−∑(v,s)∈Ef(v,s)=∑(v,t)∈Ef(v,t)−∑(t,v)∈Ef(t,v)|f| = \sum_{(s,v) \in E} f(s,v) - \sum_{(v,s) \in E} f(v,s) = \sum_{(v,t) \in E} f(v,t) - \sum_{(t,v) \in E} f(t,v).

A maximum flow is an ss-tt flow with the largest value. If every c(e)c(e) is an integer, a maximum flow in which every f(e)f(e) is an integer can be found in polynomial time. Two such algorithms are the Edmonds-Karp Algorithm, which runs in O(VE2)O(VE^2), and Dinic's Algorithm, which runs in O(V2E)O(V^2 E).

A function p:E→{0,1}p : E \rightarrow \{0, 1\} is also given. An ss-tt flow ff that satisfies the following conditions is called a parity constraint flow.

  • For every e∈Ee \in E, f(e)f(e) is an integer.
  • f(e)≡p(e)mod  2f(e) \equiv p(e) \mod 2.

The parity constraint flow with the largest value is called a parity constraint maximum flow.

Given a flow network (G,c,s,t)(G, c, s, t) and a function pp, find a parity constraint maximum flow.

Input

NN and MM are the number of vertices and edges of the graph G=(V,E)G=(V, E).

uiu_i and viv_i mean that there is an edge from uiu_i to viv_i, and cic_i and pip_i are c(ui,vi)c(u_i, v_i) and p(ui,vi)p(u_i, v_i), respectively.

The input is given in the following format.

N M s t
u_1 v_1 c_1 p_1
⋯
u_M v_M c_M p_M

Output

On the first line, print the value of the parity constraint maximum flow ff. If no parity constraint flow exists, print -1.

If a parity constraint flow exists, print f(ui,vi)f(u_i, v_i) on line i+1i+1 for each i=1,⋯ ,Mi = 1, \cdots, M.

If there are multiple answers, print any of them.

Constraints

  • 2≤N≤3002 \le N \le 300
  • 1≤M≤23 0001 \le M \le 23\,000
  • 1≤s,t≤N1 \le s, t \le N, s≠ts \ne t
  • 1≤ui,vi≤N1 \le u_i, v_i \le N, ui≠viu_i \ne v_i
  • ui≠tu_i \ne t, vi≠sv_i \ne s
  • 1≤ci≤1 000 000 0001 \le c_i \le 1\,000\,000\,000
  • pi∈{0,1}p_i \in \{0, 1\}
  • No graph has two or more edges between the same pair of vertices.
  • All numbers in the input are integers.

Examples4

  1. Example 1

    Input
    3 2 1 3
    1 2 5 0
    2 3 7 0
    
    Expected output
    4
    4
    4
    
  2. Example 2

    Input
    4 5 1 4
    1 2 4 1
    2 4 4 0
    2 3 5 1
    1 3 4 0
    3 4 4 1
    
    Expected output
    5
    3
    2
    1
    2
    3
    
  3. Example 3

    Input
    3 2 1 3
    1 2 2 1
    2 3 2 0
    
    Expected output
    -1
    
  4. Example 4

    Input
    10 13 1 10
    1 2 1 1
    1 3 1 1
    1 4 1 1
    1 5 1 1
    1 6 1 1
    2 7 1 1
    3 7 1 1
    4 8 1 1
    5 8 1 1
    6 9 1 1
    7 9 2 0
    8 9 2 0
    9 10 5 1
    
    Expected output
    5
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    2
    2
    5