1. HDU 1151 Air Raid
The topic description is a bit long, that is, a direction-free graph, at least a few times to walk the entire picture of the edge traversal. Typical minimum path overlay, the answer is vertex number-maximum number of matches
#include <set> #include <map> #include <list> #include <stack> #include <queue> #include <ctime> #include <cmath> #include <cstdio> #include <vector> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #pragma comment (linker, " /stack:1024000000,1024000000 ") #define IT iterator#define PB (x) push_back (x) #define CLR ( A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;typedef unsigned long long Ull;typedef vector<int> vint;typedef vector<ll> vll;typedef vector& lt;ull> vull;typedef set<int> sint;typedef set<ull> sull; const int MAXN = 150;vint g[maxn];int match[maxn];bool vis[maxn];int n,m;bool findpath (int u) {for (int i = 0; i < G[u].size (); i++) {int v = g[u][i]; if (!vis[v]) {vis[v] = 1; if (match[v] = =-1 | | findpath (MATCH[V])) {Match[v] = u; return true; }}} return false;} int Hungary () {int ans = 0; CLR (match,-1); for (int i = 1; I <= n; i++) {CLR (vis,0); if (Findpath (i)) ans++; } return ans; int main () {int T; cin>>t; while (t--) {CLR (vis,0); for (int i = 0; i < MAXN; i++) g[i].clear (); cin>>n>>m; for (int i = 0; i < m; i++) {int A, B; scanf ("%d%d", &a,&b); G[a]. PB (b); } cout<<n-hungary () <<endl; }}
2. HDU 1179 ollivanders:makers of Fine wands since 382 BC.
The topic description is very long, did not read, roughly look at the input output, meaning should be the stick and the person's maximum match.
#include <set> #include <map> #include <list> #include <stack> #include <queue> #include <ctime> #include <cmath> #include <cstdio> #include <vector> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #pragma comment (linker, " /stack:1024000000,1024000000 ") #define IT iterator#define PB (x) push_back (x) #define CLR ( A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;typedef unsigned long long Ull;typedef vector<int> vint;typedef vector<ll> vll;typedef vector& lt;ull> vull;typedef set<int> sint;typedef set<ull> sull; const int MAXN = + 5;int mk[maxn];int nx,ny;int cx[maxn],cy[maxn];int g[maxn][maxn];int path (int u) {for (int v = 1; V <= NY; v++) {if (G[u][v] &&!mk[v]) {mk[v] = 1; if (cy[v] = = 1 | | path (CY[V])) {Cx[u] = V; CY[V] = u; return 1; }}} return 0;} int Maxmatch () {int res = 0; CLR (cx,-1); CLR (cy,-1); for (int i = 1; I <= nx; i++) {if (cx[i] = = 1) {CLR (mk,0); Res + = path (i); }} return res;} int main () {while (Cin>>nx>>ny) {CLR (g,0); for (int i = 1; I <= ny; i++) {int k; scanf ("%d", &k); for (int j = 0; J < K; J + +) {int b; scanf ("%d", &b); G[I][B] = 1; }} cout<<maxmatch () <<endl; }}
3. HDU 1281 Board Game
This topic can be seen as a binary graph matching, then a binary graph matching. But when building the edge, consider that the car will attack each other as long as it is in the same row or column, so you can build an edge between the x-coordinate and the y-coordinate, so that there will only be one car on one row and one column for a coordinate. After solving this problem, all that is left is the template processing calculation, and then the enumeration removes each point in turn to see if the maximum match changes.
#include <set> #include <map> #include <list> #include <stack> #include <queue> #include <ctime> #include <cmath> #include <cstdio> #include <vector> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #pragma comment (linker, " /stack:1024000000,1024000000 ") #define IT iterator#define PB (x) push_back (x) #define CLR ( A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;typedef unsigned long long Ull;typedef vector<int> vint;typedef vector<ll> vll;typedef vector& lt;ull> vull;typedef set<int> sint;typedef set<ull> sull; const int MAXN = + 5;const int MAXM = 10000 + 5;int mk[maxn];int nx,ny,m;int cx[maxn],cy[maxn];int G[MAXN][MAXN];p air& lt;int,int> p[maxm];int path (int u) {for(int v = 1; v <= NY; v++) {if (G[u][v] &&!mk[v]) {mk[v] = 1; if (cy[v] = = 1 | | path (CY[V])) {Cx[u] = V; CY[V] = u; return 1; }}} return 0;} int Maxmatch () {int res = 0; CLR (cx,-1); CLR (cy,-1); for (int i = 1; I <= nx; i++) {if (cx[i] = = 1) {CLR (mk,0); Res + = path (i); }} return res;} int main () {int t = 1; while (cin>>nx>>ny>>m) {CLR (g,0); for (int i = 0; i < m; i++) {int A, B; scanf ("%d%d", &a,&b); P[i].first = A; P[i].second = b; G[A][B] = 1; } int ans = Maxmatch (); int cnt = 0; for (int i = 0; i < m; i++) {int a = P[i].first; int b = P[i].second; G[A][B] = 0; int tmp = Maxmatch (); if (tmp < ans) cnt++; G[a][b] = 1; } printf ("Board%d has%d important blanks for%d chessmen.\n", T++,cnt,ans); }}
A summary of the two-figure exercises