题目链接....................................................................
AC代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <cmath> using namespace std; typedef long long ll; const int MAX_V = 2200; int V; vector<int>G[MAX_V]; int match[MAX_V]; bool used[MAX_V],vis[MAX_V]; void add_edge(int u,int v) { G[u].push_back(v); G[v].push_back(u); } bool dfs(int v) { used[v] = true; vis[v] = true; for(int i = 0; i < G[v].size(); i++) { int u = G[v][i],w = match[u]; vis[u] = true; if(w < 0 || !used[w] && dfs(w)) { match[v] = u; match[u] = v; return true; } } return false; } int solve() { int res = 0; memset(match,-1,sizeof(match)); for(int v = 0; v < V; v++) { if(match[v] < 0) { memset(used,0,sizeof(used)); if(dfs(v)) res++; } } return res; } int main() { int n,m,p,x,y; while(~scanf("%d %d %d",&n,&m,&p) && (n | m | p)) { for(int i = 0; i < n + m; i++) G[i].clear(); for(int i = 0; i < p; i++) { scanf("%d %d",&x,&y); x--; y--; add_edge(x,n + y); } V = n + m; printf("%d ",solve()); memset(vis,0,sizeof(vis)); for(int i = 0; i < n; i++) { if(match[i] == -1) dfs(i); } for(int i = 0; i < V; i++) { if(vis[i] && i >= n) printf("c%d ",i - n + 1); if(!vis[i] && i < n) printf("r%d ",i + 1); } puts(""); } return 0; }