Parity Constraint Maximum Flow
Time limit1sMemory limit1024 MB
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.
Problem
A flow network is defined as follows.
- is a directed graph .
- is a function giving the capacity of each edge.
- are the source and the sink, respectively, and .
An - flow on a flow network satisfies the following.
- For every , .
- For every , .
The value of the flow is .
A maximum flow is an - flow with the largest value. If every is an integer, a maximum flow in which every is an integer can be found in polynomial time. Two such algorithms are the Edmonds-Karp Algorithm, which runs in , and Dinic's Algorithm, which runs in .
A function is also given. An - flow that satisfies the following conditions is called a parity constraint flow.
- For every , is an integer.
- .
The parity constraint flow with the largest value is called a parity constraint maximum flow.
Given a flow network and a function , find a parity constraint maximum flow.
Input
and are the number of vertices and edges of the graph .
and mean that there is an edge from to , and and are and , 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 . If no parity constraint flow exists, print -1.
If a parity constraint flow exists, print on line for each .
If there are multiple answers, print any of them.
Constraints
- ,
- ,
- ,
- No graph has two or more edges between the same pair of vertices.
- All numbers in the input are integers.