Hdu 3277 (two points + Max Stream + split + offline processing + template problem ...)

Source: Internet
Author: User

Marriage Match III

Time limit:10000/4000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1971 Accepted Submission (s): 583


Problem descriptionpresumably, you have known the question of stable marriage match. A girl would choose a boy; It is similar as the ever game of Play-house. What's a happy time as so many friends play together. And it is normal, a fight or a quarrel breaks out, but we'll still play together after that, because we are kids.

Now, there is 2n kids, n boys numbered from 1 to N, and n girls numbered from 1 to N. As you know, ladies first. So, every girl can choose a boy first, with whom she had not quarreled, to make up a family. Besides, the girl X can also choose Boy Z to is her boyfriend when she friend, girl Y have not quarreled with him. Furthermore, the friendship is mutual, which means a and C were friends provided that A and B were friends and B and C are f Riend.

Once every girl finds their boyfriends they would start a new round of this game-marriage match. At the end of each round, every girl would start to find a new boyfriend, who she had not chosen before. So the game goes on. On the other hand, with order to play more times of marriage match, every girl can accept any K boys. If a girl chooses a boy, the boy must accept his unconditionally whether they had quarreled before or not.

Now, here's the question for you, how many rounds can these 2n kids totally play this game?

Inputthere is several test cases. First is an integer T, means the number of test cases.
Each test case is starts with three integer n, m, K and F in a line (3<=n<=250, 0<m<n*n, 0<=f<n). n means there is 2*n children, n Girls (number from 1 to n) and N Boys (number from 1 to n).
Then M lines follow. Each line contains the numbers a and B, means girl A and boy B had never quarreled with each other.
Then f lines follow. Each line contains the numbers C and D, means girl C and girl D are good friends.

Outputfor each case, output a number on one line. The maximal number of marriage Match the children can play.

Sample Input14 5 1 21 12 33 24 24 41 42 3

Sample OUTPUT3

Authorstarvae This question test instructions HDU 3081 almost the same, more than a condition, that is, girls can also choose K of their own do not like the boys, so equal to a female more than a capacity limit, so can not be enough for the two-point diagram, the girls will be split, will girls I and girls n+i With a capacity of the side of K, I girls and like the boys with a capacity of 1 side, i+n and do not like the boy with a capacity of 1 side, and then the two-point enumeration perfect matching upper limit, but this problem still card time, so we want to take the male and female students to offline processing, first of all the ancestor node and the boys paired Then look for their children's nodes for pairing, so that you can skip a layer of loops. own dinic template too, found on the internet a big god template. Don't want to learn ISAP.
#include <cstdio>#include<cstring>#include<queue>#include<algorithm>using namespacestd;Const intMaxnode =805;Const intMAXEDGE = maxnode*Maxnode;typedefintType;ConstType INF =0x3f3f3f3f;structedge{intu, v;    Type cap, Flow; Edge () {} Edge (intUintV, type cap, type flow) {         This->u =u;  This->v =v;  This->cap =cap;  This->flow =flow; }};structdinic{intN, m, S, t;    Edge Edges[maxedge]; intFirst[maxnode]; intNext[maxedge]; BOOLVis[maxnode];    Type D[maxnode]; intCur[maxnode]; Vector<int>cut; voidInitintN) { This->n =N; memset (First,-1,sizeof(first)); M=0; }    voidAdd_edge (intUintV, Type cap) {Edges[m]= Edge (U, V, Cap,0); NEXT[M]=First[u]; First[u]= m++; EDGES[M]= Edge (V, U,0,0); NEXT[M]=First[v]; FIRST[V]= m++; }    BOOLBFs () {memset (Vis,false,sizeof(VIS)); Queue<int>Q;        Q.push (s); D[s]=0; Vis[s]=true;  while(!Q.empty ()) {            intU =Q.front ();            Q.pop ();  for(inti = First[u]; I! =-1; i =Next[i]) {Edge& e =Edges[i]; if(!VIS[E.V] && e.cap >e.flow) {VIS[E.V]=true; D[E.V]= D[u] +1;                Q.push (E.V); }            }        }        returnVis[t]; } Type Dfs (intu, Type a) {        if(U = = T | | a = =0)returnA; Type Flow=0, F;  for(int&i = Cur[u]; I! =-1; i =Next[i]) {Edge& e =Edges[i]; if(D[u] +1= = D[E.V] && (f = dfs (e.v, Min (A, e.cap-e.flow))) >0) {E.flow+=F; Edges[i^1].flow-=F; Flow+=F; A-=F; if(A = =0) Break; }        }        returnflow; } Type Maxflow (intSintt) { This->s =s;  This->t =T; Type Flow=0;  while(BFS ()) { for(inti =0; I < n; i++) Cur[i]=First[i]; Flow+=Dfs (S, INF); }        returnflow; }    voidmincut () {cut.clear ();  for(inti =0; I < m; i + =2)        {            if(VIS[EDGES[I].U] &&!VIS[EDGES[I].V]) cut.push_back (i); }}} Gao;Const intN =251;BOOLVis[n][n];intFather[n];intGirl[n*n],boy[n*n];///here M belongs to (0,n*n)intN,m,k,f,src,des;int_find (intx) {    if(father[x]!=x) {father[x]=_find (father[x]); }    returnfather[x];}voidBuildintc) {Gao.init (3*n+2);  for(intI=1; i<=n; i++) {Gao.add_edge (src,i,c); Gao.add_edge (I,n+i,k); Gao.add_edge (2*n+i,des,c); }     for(intI=1; i<=n; i++)    {         for(intj=2*n+1; j<=3*n; J + +)        {            if(Vis[i][j]) Gao.add_edge (I,j,1); ElseGao.add_edge (N+i,j,1); }    }}intMain () {inttcase; scanf ("%d",&tcase);  while(tcase--) {scanf ("%d%d%d%d",&n,&m,&k,&f); memset (Vis,false,sizeof(VIS)); SRC=0, des =3*n+1;  for(intI=1; i<=n; i++) Father[i] =i; /*for (int i=1;i<=m;i++) {//tle int u,v;             scanf ("%d%d", &u,&v);             V+=2*n;         VIS[U][V] = true; }*/         for(intI=1; i<=m; i++) {scanf ("%d%d",&girl[i],&Boy[i]); Boy[i]+=2*N; }         for(intI=1; i<=f; i++)        {            intu,v; scanf ("%d%d",&u,&v); intA = _find (u), B =_find (v); if(a!=b) Father[a]=b; }         for(intI=1; i<=m; i++) {vis[_find (Girl[i])][boy[i]=true; }         for(intI=1; i<=n; i++)///preprocess All Relationships        {             for(intj=2*n+1; j<=3*n; J + +)            {                if(Vis[i][j])Continue; if(Vis[_find (i)][j]) vis[i][j] =true; }        }        /*for (int i=1;i<=n;i++) {//tle for (int j=1;j<=n;j++) {if (_find (i) ==_find (j)) { for (int k=2*n+1;k<=3*n;k++) {if (vis[i][k]| |                    VIS[J][K]) vis[i][k] = vis[j][k] = 1; }                }            }        }*/        intL =0, r = N,ans =0;  while(l<=r) {intMid = (l+r) >>1;            Build (mid); if(Gao. Maxflow (src,des) ==mid*N) {ans=mid; L= mid+1; }            Elser = mid-1; } printf ("%d\n", ans); }    return 0;}

Hdu 3277 (two points + Max Stream + split + offline processing + template problem ...)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.