There is a sequence of work to complete. Now there are two machines A, B, and machine A in n modes, and machine B in M modes, each task can be completed in the mode on machine A, or in the B mode of machine B. You need to restart the machine when changing the mode and ask you the minimum number of times to restart the machine.
Train of Thought: This question can regard the mode of two machines as a bipartite graph, and connect the mode of one working machine to another, then obtain the minimum vertex overwrite of the Bipartite Graph (use the least vertex to connect all edges to at least a vertex (the least vertex overwrite) = The maximum matching of the Bipartite Graph ), note that the mode 0 must be ignored (because the initial mode of the machine is 0)
Summary: This question is hard to think about, but there is still one thing that can provide us with clues: two machines, two machines --- the bipartite graph? This idea is quite good, and the awareness of the Bipartite Graph has to be strengthened.
Code:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 200;struct edge{ int u,v,next;}e[maxn * maxn];int head[maxn],match[maxn],n,m,K,tot;bool vis[maxn];void init();void solve();void add_edge(int u,int v);bool dfs(int u);int hungrian();int main(){ while(scanf("%d",&n) != EOF && n){ scanf("%d%d",&m,&K); init(); for(int i = 1;i <= K;i ++){ int u,v; scanf("%d%d%d",&u,&u,&v); if(u && v)add_edge(u,v + n); } solve(); } return 0;}void init(){ int size = n + m; for(int i = 0;i < size;i ++) head[i] = match[i] = -1; tot = 0;}void add_edge(int u,int v){ e[tot].u = u , e[tot].v = v; e[tot].next = head[u],head[u] = tot ++;}bool dfs(int u){ for(int i = head[u];i != -1;i = e[i].next){ int v = e[i].v; if(!vis[v]){ vis[v] = true; int tmp = match[v]; match[v] = u; if(tmp == -1 || dfs(tmp)) return true; match[v] = tmp; } } return false;}int hungrian(){ int size = n + m,max_match = 0; for(int i = 0;i < size;i ++){ memset(vis,false,sizeof(vis)); if(dfs(i)) max_match ++; } return max_match;}void solve(){ int ret = hungrian(); printf("%d\n",ret);}