Poj1719 shooting contest feasible stream of the upstream and downstream Network

Source: Internet
Author: User
The orthodox solution of this question should be matching. However, you can also use the lower-bound network stream to solve the problem. If at least one row is shot, the lower bound is 1 upper bound INF. Each column can only be shot at one, so the upper and lower bounds are 1 ,. For each white lattice, there is no lower bound. The upper bound is 1. Each column is connected to the Source Vertex, with the capacity INF. Connect each row to the sink point. The capacity 0 is more white-point, and the Capacity 1 is used to find a feasible stream in the network. Shooting contest
Time limit:1000 ms   Memory limit:10000 K
Total submissions:3345   Accepted:1213   Special Judge

Description

Welcome to the annual byteland shooting contest. each competitor will shoot to a target which is a rectangular grid. the target consists of R * C squares located in R rows and C columns. the squares are colored white or black. there
Are exactly two white squares and R-2 black squares in each column. rows are consecutively labeled 1 ,.., R from top to bottom and columns are labeled 1 ,.., c from left to right. the shooter has C shots.

A volley of C shots is correct if exactly one white square is hit in each column and there is no row without white square being hit. help the shooter to find a correct volley of hits if such a volley exists.

Example
Consider the following target:



Volley of hits at white squares in rows 2, 3, 1, 4 in consecutive columns 1, 2, 3, 4 is correct.

Write a program that: verifies whether any correct volley of hits exists and if so, finds one of them.

Input

The first line of the input contains the number of data blocks X, 1 <= x <= 5. the following lines constitute x blocks. the first block starts in the second line of the input file; each next block starts directly after the previous
One.

The first line of each block contains two integers R and C separated by a single space, 2 <= r <= C <= 1000. these are the numbers of rows and columns, respectively. each of the next C lines in the block contains two integers separated by a single space. the
Integers in the input line I + 1 in the block, 1 <= I <= C, are labels of rows with white squares in the I-th Column.

Output

For the I-th block, 1 <= I <= X, your program shocould write to the I-th line of the standard output either a sequence of C Row Labels (separated by single spaces) forming a correct volley of hits at white squares in consecutive
Columns 1, 2,..., C, or one word no if such a volley does not exists.

Sample Input

24 42 43 41 31 45 51 52 43 42 42 3

Sample output

2 3 1 4NO
# Include <iostream >#include <cstdio> using namespace STD; # define maxn 20010 # define maxm 500010 # define INF 0 xffffffstruct edge {int to, C, next ;}; edge e [maxm]; int head [maxn], en, vn; int R, C, N, M; int dis [maxn]; int gap [maxn]; int St, ed; int in [maxn]; int L [maxm]; void add (int A, int B, int c) {e [En]. to = B; E [En]. C = C; E [En]. next = head [a]; head [a] = EN ++; E [En]. to = A; E [En]. C = 0; E [En]. next = head [B]; head [B] = EN ++ ;} Int ISAP (int u, int flow) {If (u = ed) return flow; Int J, mindis = vn-1, t = flow, D; for (j = head [u]; J! =-1; j = E [J]. next) {int v = E [J]. to, val = E [J]. c; If (Val> 0) {If (DIS [v] + 1 = dis [u]) {If (T <E [J]. c) d = T; else d = E [J]. c; D = ISAP (v, d); e [J]. c-= D, E [J ^ 1]. c + = D; t-= D; If (DIS [st]> = Vn) Return Flow-T; If (t = 0) break ;} if (DIS [v] <mindis) mindis = dis [v] ;}} if (t = flow) {-- gap [dis [u]; if (gap [dis [u] = 0) dis [st] = Vn; DIS [u] = mindis + 1; ++ gap [dis [u];} return Flow-T;} int maxflow () {int ret = 0; memset (GAP, 0, sizeof (gap ); Memset (DIS, 0, sizeof (DIS); Gap [0] = Vn; while (DIS [st] <VN) RET + = ISAP (St, INF); return ret;} int limitflow () {int I, st0, ed0, en0, ret = 0; st0 = ST, ed0 = ed, en0 = en; st = Vn ++, ED = Vn ++; head [st] = head [ed] =-1; for (I = 0; I <vn-2; ++ I) {If (in [I]> 0) add (St, I, in [I]); If (in [I] <0) add (I, Ed, -In [I]);} Add (ed0, st0, INF); add (st0, ed0, INF); maxflow (); for (I = head [st]; i! =-1; I = E [I]. next) if (E [I]. c) Return-1; return 1;} void solve () {int A, B; scanf ("% d", & R, & C ); vn = R + C + 2, en = 0, St = 0, ED = R + C + 1; memset (Head,-1, sizeof (head )); memset (in, 0, sizeof (in); For (INT I = 1; I <= C; I ++) {scanf ("% d ", & A, & B); add (I, A + C, 1); add (I, B + C, 1) ;}for (INT I = 1; I <= C; I ++) {Add (St, I, 0); // column upper limit 1; lower limit 1In [st] --; in [I] ++ ;} for (INT I = 1; I <= r; I ++) {Add (I + C, Ed, INF); // lower limit 1, no upper limit in [I + C] --; in [ed] ++;} If (limitflow () =-1) {printf ("n O \ n "); return;} int counts = 0; For (INT I = 0; I <C * 2; I ++) {If (E [(I * 2) ^ 1]. c) {counts ++; printf ("% d", E [I * 2]. to-C); If (counts! = C) printf ("") ;}} printf ("\ n") ;}int main () {int t; scanf ("% d", & T ); while (t --) solve (); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.