UV 11419 Sam I am (bipartite graph, minimum cut)

Source: Internet
Author: User

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 2414


Problem C
Sam I am
Input:
Standard Input

Output:Standard output

 

The world is in great danger !!Mental's forcesHave returned to Earth to eradicate humankind. Our last hope to stop this great edevil isSam "serious" Stone. Equipped with various powerful weapons, serious Sam starts his mission to destroy the forces of edevil.

After fighting two days and three nights, Sam is now in front of the temple koptos where mental's general ugh Zan III is waiting for him. but this time, he has a serious problem. he is in temporary age of ammo and a lot of enemies crawling inside the temple waiting for him. after rounding thetemple Sam finds that the temple is in rectangle shape and he has the locations of all enemies in the temple.

All of a sudden he realizes that he can kill the enemies without entering the temple using the great cannon ball which spits out a gigantic ball bigger than him killing anything it runs into and keeps on rolling it finally explodes. but the cannonball can only shoot horizontally or vertically and all the enemies along the path of that cannon ball will be killed.

Now he wants to save as your cannon bils as possible for fighting with mental. so, he wants to know the minimum number of cannon balland the positions from which he can shoot the cannonballto eliminate all enemies from outside that temple.

 

Input

Here, the temple is defined as a RXC grid. the first line of each test case contains 3 integers: R (0 <r <1001), C (0 <C <1001) representing the grid of Temple (R means number of row and C means number of column of the grid) and the number of enemies N (0 <n <1000001) inside the temple. after that there are n lines each of which contains 2 integers representing the position of the enemies in that temple. each test case is followed by a new line (except t the last one ). input is terminated when r = c = n = 0. the size of the input file is found 1.3 MB.

 

Output

For each test case there will be one line output. First print the minimum number (M) Of cannonbils needed to wipe out the enemies followed by a single space and thenMPositions from which he can shoot those cannonbils. For shooting horizontally print"R"Followed by the row number and for vertical shooting print"C"Followed by the column number. If there is more than one solution any one will do.

 

Sample input output for sample input

4 4 3

1 1

1 4

3 2

 

4 4 2

1 1

2 2

 

0 0 0

 

2 R1 r3

2 R1 r2

 

Problemsetter: Syed monowar Hossain

Special thanks: Derek kisman


 

Question:
There are n targets in the r×c matrix. One row or column can be eliminated at a time, and the minimum number of elimination times and their solutions can be obtained. Any one of the multiple solutions can be output.
Analysis:
Obviously, the bipartite graph is the least covered and can be parsed by network streams. One edge with a capacity of 1 is connected from the source point to each row, and one edge with a capacity of 1 is connected from each column to the sink point. One edge with a capacity of 1 is corresponding to the row and column of each target, the minimum cut is the solution.
The minimum cut can obviously be solved using the maximum stream. To find the final minimum cut, you also need to run one DFS in the residual network to open the S and T parts.


# Include <cstdio> # include <iostream> # include <cstdlib> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (x) (x) * (x) # define ll long # define ITN int # define INF 0x3f3f3f3f # define PI 3.1415926535897932384626 # define EPS 1e-10 # define maxm 2500007 # define maxn 2007 using namespace STD; int FIR [maxn]; int U [maxm], V [maxm], Cap [maxm], flow [maxm], NEX [maxm]; int e_max; int LV [maxn], ITER [maxn], Q [maxn]; void add_edge (INT _ u, int _ v, int _ w) {int e; E = e_max ++; U [e] = _ u; V [e] = _ v; CAP [e] = _ w; nex [e] = FIR [U [e]; FIR [U [e] = E; E = e_max ++; U [e] = _ V; V [e] = _ u; CAP [e] = 0; NEX [e] = FIR [U [e]; FIR [U [e] = E ;} void dinic_bfs (INT s) {int F, R; memset (LV,-1, sizeof LV); LV [s] = 0; Q [f = r = 0] = s; while (F <= r) {Int x = Q [f ++]; for (itn e = FIR [X]; ~ E; E = NEX [e]) {If (Cap [e]> flow [e] & LV [V [e] <0) {LV [V [e] = lv [U [e] + 1; Q [++ R] = V [e] ;}}} int dinic_dfs (INT _ u, int T, int _ f) {If (_ u = T) return _ F; For (Int & E = ITER [_ u]; ~ E; E = NEX [e]) {If (Cap [e]> flow [e] & LV [U [e] <LV [V [e]) {int _ d = dinic_dfs (V [e], T, min (_ F, Cap [e]-flow [e]); If (_ d> 0) {flow [e] + = _ d; flow [E ^ 1]-= _ d; return _ d ;}} return 0 ;}int max_flow (INT s, int t) {memset (flow, 0, sizeof flow); int total_flow = 0; For (;) {dinic_bfs (s); If (LV [T] <0) return total_flow; memcpy (ITER, fir, sizeof ITER); int _ F; while (_ f = dinic_dfs (S, T, INF)> 0 )) total_flow + = _ F;} return t Otal_flow;} bool vis [maxn]; void DFS (INT _ u) {vis [_ u] = true; For (int e = FIR [_ u]; ~ E; E = NEX [e]) {If (Cap [e]> flow [e] &! Vis [V [e]) DFS (V [e]) ;}} int main () {# ifndef online_judge freopen ("/home/fcbruce/documentation/code/T", "r", stdin); # endif // online_judge int R, C, N, _ u, _ v; while (scanf ("% d", & R, & C, & N), R | c | N) {int S = 0, T = R + C + 1; e_max = 0; memset (FIR,-1, sizeof fir); For (INT I = 1; I <= r; I ++) add_edge (S, I, 1); For (INT I = R + 1; I <= R + C; I ++) add_edge (I, T, 1); For (ITN I = 0; I <n; I ++) {scanf ("% d", & _ u, & _ V); add_edge (_ u, _ v + R, 1);} print F ("% d", max_flow (S, T); memset (VIS, 0, sizeof vis); DFS (s); For (INT I = 1; I <= r; I ++) if (! Vis [I]) printf ("R % d", I); For (INT I = R + 1; I <= R + C; I ++) if (vis [I]) printf ("C % d", I-r); puts ("") ;}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.