Question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 239
Idea: maximum matching of a bipartite graph ~~~ Joining Table A through ~~
#include <iostream>#include <cstring>using namespace std;int num;int n,k;int s;struct node{ int u,v,w; int next;}map[10010];int head[510];int match[510];int used[510];bool solve(int s){ for(int i = head[s]; i != -1; i = map[i].next) { if(used[map[i].v] == 0) { used[map[i].v] = 1; if(match[map[i].v] == -1 || solve(match[map[i].v]) == true) { match[map[i].v] = s; return true; } } } return false;}void add_edge(int u, int v, int w){ map[s].u = u; map[s].v = v; map[s].w = 1; map[s].next = head[u]; head[u] = s++;} int main(){ cin>>num; int a,b; while(num--) { s = 0; cin>>n>>k; memset(head, -1, sizeof(head)); memset(map, 0, sizeof(map)); memset(match,-1,sizeof(match)); for(int i=1; i<=k; i++) { cin>>a>>b; add_edge(a,b,1); } int res = 0; for(int i=1; i<=n; i++) { memset(used, 0, sizeof(used)); if(solve(i)) res++; } cout<<res<<endl; } return 0;}
Nyoj's 239-month-old problem: maximum matching of bipartite graphs