Click to open link
Test instructions: With POJ 1904 very similar to a topic, POJ will a complete matching map to you, and gave you a set of possible situations, very simple, but this problem, give neither complete match nor give feasible matching scheme, difficult not to
Idea: Just started to think that is and 19,041 mold, but the difficulty rises really fast, looked at a bit is the 13 of the multi-school topic, over 10 several, imagine this difficulty is not I can be a drop, just think of only add Prince make it complete match, then WA, To say why the need to become a complete match, because the number of princes and princesses in this topic is not necessarily the same, even if the same may not be a complete match, so that there will be princes and princesses can not find their partner, but our subject requirements are each prince may marry princess conditions, the maximum match unchanged, Then like the second set of examples, the Prince 1 married Princess 1, then the Prince 1 can also marry the Princess 2, these two will not make the biggest match change, according to the idea of 1904 we require strong unicom components, in a component of each prince can marry the remaining part of all the princess without changing the maximum match, for what? The Princess and the Prince in this component are equal, and the strong Unicom components can reach each other, so they can change their partners. Now look at this problem, for no matching princess, no change the maximum match case, all like this princess Prince can marry her, then our purpose becomes how to make these princes like Princess and this princess in a Unicom component, then for no match prince, He can marry any princess he likes without changing the maximum match, and then our goal becomes how to be like these princesses Prince with this single dog in a unicom component, so we can virtual some Prince and princess, here only one, another operation is the same, for example, the Princess has k an unmatched, Then add K virtual Prince, these princes even to all the princess, this will certainly ensure that all the princesses are matched, it is obvious that, then why the prince to all the princess, this is to the SCC when all like this Princess Prince and this single dog in a Unicom component, the princess is also a truth PS: The code is too ugly, don't look unless you're interested.
#include <queue> #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h > #include <iostream> #include <algorithm>using namespace std;typedef long long ll;const int inf= 0x3f3f3f3f;const int maxn=10010;//Strong unicom startvector<int> g[maxn];vector<int> rG[maxn];vector<int> vs ; bool Used[maxn];int cmp[maxn],v;void add_edge (int from,int to) {g[from].push_back (to); Rg[to].push_back (from);} void Dfs (int v) {used[v]=1; for (int i=0;i<g[v].size (); i++) {if (!used[g[v][i])) DFS (G[v][i]); } vs.push_back (v);} void Rdfs (int v,int k) {used[v]=1; Cmp[v]=k; for (int i=0;i<rg[v].size (); i++) {if (!used[rg[v][i]) Rdfs (rg[v][i],k); }}int SCC () {memset (used,0,sizeof (used)); Vs.clear (); for (int v=0;v<v;v++) if (!used[v]) Dfs (v); memset (used,0,sizeof (used)); int k=0; for (int i=vs.size () -1;i>=0;i--) {if (!used[vs[i]) Rdfs (vs[i],k++); } return k;} Strong Unicom Endint NUM[MAXN];//network stream startstruct edge{int to,cap,rev; Edge (int a,int b,int c) {to=a;cap=b;rev=c;}}; Vector<edge>gg[maxn];int level[maxn],iter[maxn];void addedge (int from,int to,int cap) {GG[from].push_back (Edge (To,cap,gg[to].size ())); Gg[to].push_back (Edge (From,0,gg[from].size ()-1));} void BFs (int s) {memset (level,-1,sizeof (level)); queue<int>que;level[s]=0; Que.push (s); while (!que.empty ()) {int V=que.front (); Que.pop (); for (unsigned int i=0;i<gg[v].size (); i++) {Edge &e=GG[v][i]; if (e.cap>0&&level[e.to]<0) {level[e.to]=level[v]+1; Que.push (e.to); }}}}int dfs (int v,int t,int f) {if (v==t) return F; for (int &i=iter[v];i<gg[v].size (); i++) {Edge &e=GG[v][i]; if (e.cap>0&&level[v]<level[e.to]) {int D=dfs (e.to,t,min (F,e.cap)); if (d>0) {e.cap-=d; Gg[e.to][e.rev].cap+=d; return D; }}} return 0;} void Max_flow (int s,int t) {int flow=0; while (1) {BFS (s); if (level[t]<0) return; memset (Iter,0,sizeof (ITER)); int F; while ((F=dfs (S,t,inf)) >0) flow+=f; }}//Network Stream Endint vis[maxn],vis1[maxn];int main () {int t,cas=1,n,m,a,b,c; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&m); memset (vis,0,sizeof (VIS)); memset (vis1,0,sizeof (VIS1)); for (int i=0;i<maxn;i++) {g[i].clear (); Rg[i].clear (); Gg[i].clear (); } vs.clear (); for (int i=1;i<=2000;i++) Addedge (0,i,1); for (int i=1;i<=m;i++) Addedge (i+2000,2000+m+1,1); for (int i=1;i<=n;i++) {scanf ("%d", &c); while (c--) {scanf ("%d", &a); Add_edge (i,a+2000); Addedge (i,a+2000,1); }} max_flow (0,2000+m+1); int CNT=N,CNT1=m; for (int i=1;i<=n;i++) {for (unsigned int j=0;j<gg[i].size (); j + +) {edge e=gg[i][j]; if (e.cap==0&&e.to>=2001&&e.to<=2000+m) {vis1[i]=1; Vis[e.to]=1;break; }}} for (int i=1;i<=m;i++) if (vis[i+2000]==0) cnt++; for (int j=1;j<=n;j++) if (vis1[j]==0) cnt1++; for (int i=0;i<maxn;i++) gg[i].clear (); for (int i=1;i<=cnt;i++) Addedge (0,i,1); for (int i=1;i<=cnt1;i++) Addedge (i+2000,2000+cnt1+1,1); for (int i=n+1;i<=cnt;i++) {for (int j=1;j<=cnt1;j++) {Addedge (i,j+2000,1); Add_edge (i,j+2000); }} for (int i=m+1;i<=cnt1;i++) {for (int j=1;j<=cnt;j++) {Addedge (j,i+2000,1) ; Add_edge (j,i+2000); }} for (int i=1;i<=n;i++) {for (unsigned int j=0;j<g[I].size (); j + +) {int t=g[i][j]; Addedge (i,t,1); }} max_flow (0,2000+cnt1+1); for (int i=1;i<=cnt;i++) {for (unsigned int j=0;j<gg[i].size (); j + +) {edge e=gg[i][j]; if (e.cap==0) {Add_edge (e.to,i); }}} V=2000+cnt1; int ANS=SCC (); printf ("Case #%d:\n", cas++); for (int i=1;i<=n;i++) {int sum=0; for (unsigned int j=0;j<g[i].size (); j + +) {int t=g[i][j]; if (cmp[i]==cmp[t]&&t-2000>=1&&t-2000<=m) {num[sum++]=t; }} if (sum==0) printf ("%d\n", sum); else printf ("%d", sum); Sort (num,num+sum); for (int k=0;k<sum;k++) {if (k!=sum-1) printf ("%d", num[k]-2000); else printf ("%d\n", num[k]-2000); } } } return 0;}
HDU 4685 components + network flow