is interested in pets? There is a very famous pets-in-the-center of the ACM city. There is totally m pets in the shop, numbered from 1 to M. One day, there is n customers in the shop, which is numbered from 1 to N. In order to sell pets to as more customers as possible, each customer is just allowed to buy at the most one pet. Now, your task was to help the manager to sell as more pets as possible.
Every customer would not buy the pets he/she are not interested in it, and every customer would as-buy one pet that he /she is interested in if possible.
Input
There is a single integer t in the first line of the the test data indicating, which there is T (t≤100) test cases. In the first line of all test case, there is three numbers n, M (0≤n,m≤100) and E (0≤e≤n*m). Here, N and M represent the number of customers and the number of pets respectively.
In the following e lines of all test case, there be, integers x (1≤x≤n), Y (1≤y≤m) indicating, customer x is not in Terested in pet y, such this x would not buy Y.
Output
For each test case, print a line containing the "test Case number" (beginning with 1) and the maximum number of pets that CA N is sold out.
Sample Input
1 2 2 2 1 2 2 1
Sample Output
Case 1:2
The main topic: There are n customers, there are M pet, and the customer has e requirements, the requirements of the content, no. I customers do not want to buy the Pet No. J. Ask how many pets can be sold. Solution thinking: You can use the maximum flow, you can use the Hungarian Hungary algorithm to find a binary map. When the maximum flow, pay attention to the split point. Establish a super source to connect all the customers, the capacity is INF, set up a super meeting point so that all pets are connected to him, the capacity is INF. Customers and pets each split into two points, with a capacity of 1, so that each customer can only buy one pet, each pet can only be purchased by one customer. Then, according to the requirements of e, establish the side between the customer and the pet, the capacity is 1, then the maximum flow. This method is more complex and time-consuming, so it is best to use the Hungarian algorithm. Maximum Flow
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;typedef Long LongllConst intN =1005;Const intOF1 = -;Const intOF2 = $;Const intFIN =505;Const intINF =0x3f3f3f3f;intN, M, E, F[n][n], S, t;structedge{intFrom, to, cap, flow; }; vector<Edge>Edges vector<int>G[n];voidInit () {s =0, t = FIN; for(inti =0; i < N; i++) g[i].clear (); Edges.clear ();memset(F,0,sizeof(f));voidAddedge (intFromintTo,intCapintFlow) {Edges.push_back (Edge) {from, to, Cap,0}); Edges.push_back (Edge) {To, from,0,0});inttemp = Edges.size (); G[from].push_back (Temp-2); G[to].push_back (Temp-1);}voidInput () {intA, B; for(inti =0; I < E; i++) {scanf("%d%d", &a, &b); F[A][B] =1; } for(inti =1; I <= N; i++) {Addedge (0, I, INF,0); Addedge (i, i + OF1,1,0); } for(inti =1; I <= m; i++) {Addedge (i + OF2, i + OF2 + OF1,1,0); Addedge (i + OF2 + OF1, FIN, INF,0); } for(inti =1; I <= N; i++) { for(intj =1; J <= M; J + +) {if(!f[i][j]) {Addedge (i + OF1, j + OF2,1,0); } } }}intVis[n], d[n];intBFS () {memset(Vis,0,sizeof(VIS));//for (int i = 0; i < FIN; i++) d[n] = INF; Queue<int>Q; Q.push (s); D[s] =0; Vis[s] =1; while(! Q.empty ()) {intU = Q.front (); Q.pop (); for(inti =0; I < g[u].size (); i++) {Edge &e = edges[g[u][i]];if(!vis[e.to] && e.cap > E.flow) {Vis[e.to] =1; D[e.to] = D[u] +1; Q.push (e.to); } } }returnVIS[T];}intCur[n];intDFS (intUintA) {if(U = = T | | a = =0)returnAintFlow =0, F; for(int&i = Cur[u]; I < g[u].size (); i++) {Edge &e = edges[g[u][i]];if(D[u] +1= = D[e.to] && (f = DFS (e.to, Min (A, e.cap-e.flow))) >0) {E.flow + = f; edges[g[u][i]^1].flow-= f; Flow + + F; A-= f;if(A = =0) Break; } }returnFlow;}intMF () {intAns =0; while(BFS ()) {memset(cur,0,sizeof(cur)); Ans + = DFS (s, INF); }returnAns;}intMain () {intT, Case =1;scanf("%d", &t); while(t--) {printf("Case%d:", case++);scanf(" %d%d%d", &n, &m, &e); Init (); Input ();intans = MF ();printf("%d\n", ans); }return 0;}
Hungarian algorithm
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace STD;typedef__int64 ll;Const intN =505;intN, M, E, ans;intG[n][n], Vis[n], r[n];voidInput () {memsetG1,sizeof(G));memsetR0,sizeof(R));intA, B; for(inti =0; I < E; i++) {scanf("%d%d", &a, &b); G[A][B] =0; } }intFindintx) { for(inti =1; I <= m; i++) {if(G[x][i] &&!vis[i]) {Vis[i] =1;if(R[i] = =0|| Find (R[i])) {r[i] = x;return 1; } } }return 0;}voidHungary () { for(inti =1; I <= N; i++) {memset(Vis,0,sizeof(VIS));if(Find (i)) ans++; }}intMain () {intT, Case =1;scanf("%d", &t); while(t--) {printf("Case%d:", case++); Ans =0;scanf(" %d%d%d", &n, &m, &e); Input (); Hungary ();printf("%d\n", ans); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Fzu 2039 Pets (Simple binary graph + (maximum flow | | (Two-part chart))