Test instructions
For n points and M points, an n can correspond to only one m, one m can correspond to multiple n, and each n gives the set of M points that he can correspond to, and m corresponds to the minimum value of n majority.
Analysis:
Network flow + two points.
Code:
POJ 2289//sep9#include <iostream> #include <queue> using namespace std; const int maxn=2048; const int maxm=1000000; struct Edge {int v,f,nxt; }E[MAXM*2+10]; Queue<int> que; int src,sink; int g[maxn+10]; int nume; BOOL VIS[MAXN+10]; int dist[maxn+10]; int map[2048][2048]; int N,m;char tmp[4000];void addedge (int u,int v,int c) {e[++nume].v=v;e[nume].f=c;e[nume].nxt=g[u];g[u]=nume; E[++nume].v=u;e[nume].f=0;e[nume].nxt=g[v];g[v]=nume; } void Init () {memset (g,0,sizeof (g)); nume=1; } void BFs () {while (!que.empty ()) Que.pop (); Vis[src]=true; Que.push (SRC); while (!que.empty ()) {int U=que.front (); Que.pop (); for (int i=g[u];i;i=e[i].nxt) if (e[i].f&&!vis[e[i].v]) {Que.push (E[I].V); dist[e[i].v]=dist[u]+1; Vis[e[i].v]=true; }}} int dfs (int u,int delta) {if (U==sink) RetuRN Delta; int ret=0; for (int i=g[u];d elta&&i;i=e[i].nxt) if (e[i].f&&dist[e[i].v]==dist[u]+1) {int Dd=dfs ( E[i].v,min (E[i].f,delta)); E[I].F-=DD; E[I^1].F+=DD; DELTA-=DD; RET+=DD; } return ret; } int dinic (int maxx) {init (); Src=0,sink=n+m+1;int i,j;for (i=1;i<=n;++i) Addedge (src,i,1); for (i=1;i<=n;++i) for (j=1;j<=m;++j) if (Map[i] [J]==1] Addedge (i,n+j,1); for (I=1;i<=m;++i) Addedge (n+i,sink,maxx); int ret=0; while (1) {memset (vis,0,sizeof (VIS)); memset (dist,0,sizeof (Dist)); BFS (); if (!vis[sink]) break; Ret+=dfs (Src,int_max); } if (ret==n) return 1; return 0;} int main () {while (scanf ("%d%d", &n,&m) ==2) {if (n==0&&m==0) Break;int I,j;getchar (); memset (map,0, sizeof (map)); for (int i=1;i<=n;i++) {gets (TMP); int Len=strlen (TMP); for (int j=0;j<len;j++) {if (tmp[j]>= ' 0 ' &&tmp[j]<= ' 9 ') {int v=0; while (tmp[j]>= ' 0 ' &&tmp[j]<= ' 9 ') {v=v*10+tmp[j++]-' 0 '; } map[i][v+1]=1; }}}int Ans,l,r;l=0,r=n+1;while (l<r) {int mid= (l+r)/2;if (Dinic (mid) ==1) {R=mid;ans=mid;} elsel=mid+1;} printf ("%d\n", ans);} return 0; }
POJ 2289 Jamie's contact Groups two points + network stream