HDU 1281
Since each row can have one at most and each column can have one at most (the positions that cannot be placed do not affect the attack, because you did not pay attention to this sentence and used this question as a row-column coverage Model For A Long Time 0.0)
Therefore, the rows and columns are directly treated as the row and column join edges of the points in the bipartite graph X and Y sets. The complete matching is the second answer.
As for the key point of the first answer, it is the key point to enumerate whether a side can be completely matched. If not, it is the key point.
My code C ++ will wa. I don't know why. Please advise.
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int mp[105][105];int to[105];bool vis[105];int n;int dfs(int k){ for(int i=1;i<=n;i++) { if(!vis[i]&&mp[k][i]) { vis[i]=1; if(to[i]==-1||dfs(to[i])) { to[i]=k; return 1; } } } return 0;}int a[100090],b[100900];int main(){ int m,k,ca=1; while(scanf("%d%d%d",&n,&m,&k)!=EOF) { memset(mp,0,sizeof(mp)); for(int i=1;i<=k;i++) { scanf("%d%d",&a[i],&b[i]); mp[a[i]][b[i]]=1; } int ans=0; memset(to,-1,sizeof(to)); for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); ans+=dfs(i); } int ans2=0,tot=0; for(int i=1;i<=k;i++) { ans2=0; mp[a[i]][b[i]]=0; memset(to,-1,sizeof(to)); for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); ans2+=dfs(i); } if(ans2!=ans) tot++; mp[a[i]][b[i]]=1; } printf("Board %d have %d important blanks for %d chessmen.\n",ca++,tot,ans); } return 0;}
Poj 2062
The first person plays the cards in order, and the second person places the cards in order to obtain the maximum score.
It is easy to think of Bipartite Graph Matching. When creating a graph, it is enough to write a function to compare the card size.
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int mp[105][105];int to[555];bool vis[555];char s[10];char h[500];int n;int dfs(int k){ for(int i=0;i<n;i++) { if(!vis[i]&&mp[k][i]) { vis[i]=1; if(to[i]==-1||dfs(to[i])) { to[i]=k; return 1; } } } return 0;}bool cmp(char *sa,char *sb){ if(h[sa[0]]==h[sb[0]]) return h[sa[1]]>h[sb[1]]; return h[sa[0]]>h[sb[0]];}char sa[105][5],sb[105][5];int main(){ h['H']=3; h['S']=2; h['D']=1; h['C']=0; for(int i='2';i<='9';i++) h[i]=i-'2'; h['T']=8; h['J']=9; h['Q']=10; h['K']=11; h['A']=12; int cas; scanf("%d",&cas); while(cas--) { memset(mp,0,sizeof(mp)); memset(to,-1,sizeof(to)); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s",sa[i]); } for(int i=0;i<n;i++) { scanf("%s",sb[i]); } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(cmp(sb[j],sa[i])) mp[j][i]=1; } } int ans=0; for(int i=0;i<n;i++) { memset(vis,0,sizeof(vis)); ans+=dfs(i); } printf("%d\n",ans); } return 0;}
HDU 2119
The rows and columns are divided into two parts. If the intersection position is 1, an edge with a capacity of INF is connected, and the other side has a capacity of 1. The minimum cut is to eliminate the solutions of all 1.
# Include <cstdio> # include <cstring> # include <cmath> # include <iostream> # include <algorithm> # include <set> # include <map> # include <queue> # include <vector> # include <string> # define EPS 1e-12 # define INF 0x7fffffff # define maxn 22222 using namespace STD; int n, m; int en; int St, Ed; // Source Vertex and sink vertex int dis [maxn]; // dis [I], int que [9999999]; struct edge {int to, C, next ;}; edge e [9999999]; int head [maxn]; 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 BFS () {memset (DIS,-1, sizeof (DIS )); dis [st] = 0; int front = 0, rear = 0; que [rear ++] = sT; while (front <rear) {Int J = que [Front ++]; for (int K = head [J]; k! =-1; k = E [K]. next) {int I = E [K]. to; If (DIS [I] =-1 & E [K]. c) {dis [I] = dis [J] + 1; que [rear ++] = I; if (I = ed) return true ;}}} return false;} int DFS (int x, int MX) {int I, A; If (x = ed) return MX; int ret = 0; for (int K = head [X]; k! =-1 & RET <MX; k = E [K]. next) {If (E [K]. C & dis [E [K]. to] = dis [x] + 1) {int dd = DFS (E [K]. to, min (E [K]. c, Mx-RET); e [K]. c-= dd; E [k ^ 1]. c + = dd; RET + = dd ;}} if (! RET) dis [x] =-1; return ret;} void Init () {en = 0; ST = 0; // source ED = N + M + 10; // collect memset (Head,-1, sizeof (head);} void build () {int x, y, z; For (INT I = 1; I <= N; I ++) add (St, I, 1); For (Int J = 1; j <= m; j ++) add (J + N, ed, 1); For (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= m; j ++) {scanf ("% d", & X); If (x = 1) Add (I, j + N, INF) ;}} int dinic () {int TMP = 0; int maxflow = 0; while (BFS () {While (TMP = DFS (St, INF) maxflow + = TMP;} return maxflow ;} int main () {While (scanf ("% d", & N) {scanf ("% d", & M); Init (); build (); printf ("% d \ n", dinic ());}}