Nyoj 239:http://acm.nyist.net/judgeonline/problem.php?pid=239
Ural 1109:http://acm.timus.ru/problem.aspx?space=1&num=1109
Nyoj Matchmaker The problem, is the bare maximum match, very annoying is the adjacency array timeout. Use the adjacency table instead.
#include <bits/stdc++.h>using namespacestd;#defineMAXN 1005Vector<int>G[MAXN];BOOLUSE[MAXN];intMATCH[MAXN];intm,n,k;BOOLDfsintu) { for(intI=0; I<g[u].size (); i++) { if(use[g[u][i]]==false) {Use[g[u][i]]=true; if(match[g[u][i]]==-1||DFS (Match[g[u][i])) {Match[g[u][i]]=u; return true; } } } return false;}intMain () {scanf ("%d%d%d",&m,&n,&k); for(intI=0; i<k;i++) { intu,v; scanf ("%d%d",&u,&v); G[u].push_back (v); } memset (Match,-1,sizeof(match)); intAns =0; for(intI=1; i<=m;i++) {memset (use,0,sizeof(use)); if(Dfs (i)) ans++; } printf ("%d\n", ans); //printf ("%d\n", M+n-ans); return 0;}
View Code
Then the Ural, the smallest path overlay.
Test instructions
A country has m representatives, b state-owned n representatives, of which K can negotiate with delegates (one is a, one is State B), and each represents at least one pair (that is, everyone can at least find another person to negotiate), Each pair of negotiations requires a pair of phone calls (a pair of phone numbers of 1), which now makes it possible for everyone to make a minimum number of contacts for a phone call.
is to ask for the minimum logarithm. Each point must have an edge attached to it, so that the minimum number of edges-the minimum path coverage.
First ask for the maximum match (both are a pair), there may be no matching people, plus these people, such as case: Maximum match 2, there is no match on the left number 2nd. Add this man.
Formula:
Minimum path override = n+ M-2 * ans + ans;
#include <bits/stdc++.h>using namespacestd;#defineMAXN 1005Vector<int>G[MAXN];BOOLUSE[MAXN];intMATCH[MAXN];intm,n,k;BOOLDfsintu) { for(intI=0; I<g[u].size (); i++) { if(use[g[u][i]]==false) {Use[g[u][i]]=true; if(match[g[u][i]]==-1||DFS (Match[g[u][i])) {Match[g[u][i]]=u; return true; } } } return false;}intMain () {scanf ("%d%d%d",&m,&n,&k); for(intI=0; i<k;i++) { intu,v; scanf ("%d%d",&u,&v); G[u].push_back (v); } memset (Match,-1,sizeof(match)); intAns =0; for(intI=1; i<=m;i++) {memset (use,0,sizeof(use)); if(Dfs (i)) ans++; } printf ("%d\n", m+n-ans); return 0;}
View Code
Ural 1109,nyoj 239, Hungarian algorithm adjacency table