N-marriage Match ii-hdu 3081 (Max Stream)

Source: Internet
Author: User
Tags rounds

some boys and girls play a game, each girl can pick a boy to carry out the game (everyone to participate), the girl will only choose her favorite boys, and they think their friends like the boys they also like (friend's boyfriend is also my boyfriend??? , and they follow a friend's friend is also the principle of friends, ask them to play a few rounds of games (each round to choose the person can not be the same as the previous selection).   Analysis: Friends are obviously able to use and find out what each girl can connect to the boys, because the demand is the maximum number of rounds of the game, that is, in this X-round game each girl changed x different boys, each boy also changed x different girls, if the source and the girl, meeting point and the boy connected, Then the traffic must be n*x, you can use two points to find the largest x. The following is the AC code. =========================================================================================================== ==============
#include <stdio.h>#include<string.h>#include<queue>#include<stack>#include<vector>using namespacestd;Const intMAXN =207;Const intOO = 1e9+7;intG[MAXN][MAXN], LAYER[MAXN], N, M;intGIRL[MAXN*MAXN], boy[maxn*MAXN], Father[maxn];vector<int>LOVE[MAXN];intFind (intx) {    if(Father[x]! =x) father[x]=Find (father[x]); returnfather[x];}voidInIt () { for(intI=1; i<=n; i++) {Father[i]=i;    Love[i].clear (); }}voidBuidgraph (intFlowintStartintEnd) {memset (G,0,sizeof(G));  for(intI=1; i<=n; i++)    {///The source is connected to the girl, and the meeting point is connected to the boy, Flow .G[start][i] =flow; G[i+n][end] =flow; intU = Find (i);///take care not to use Father[i]        intLen =love[u].size ();  for(intj=0; j<len; J + +)        {///the flow between girls and boys is 1G[I][LOVE[U][J]] =1; }    }}BOOLBFS (intStartintEnd) {memset (Layer,0,sizeof(Layer)); Queue<int>Q;    Q.push (start); Layer[start]=1;  while(Q.size ()) {intU =Q.front ();        Q.pop (); if(U = = End)return true;  for(intv=1; v<=end; v++)        {            if(layer[v]==false&&G[u][v]) {Layer[v]= Layer[u] +1;            Q.push (v); }        }    }    return false;}intDFS (intUintMaxflow,intEnd) {    if(U = = End)returnMaxflow; intUflow =0;  for(intv=1; v<=end; v++)    {        if(layer[v]==layer[u]+1&&G[u][v]) {            intflow = min (maxflow-Uflow, G[u][v]); Flow=DFS (V, Flow, End); G[U][V]-=flow; G[v][u]+=flow; Uflow+=flow; if(Uflow = =Maxflow) Break; }    }    if(Uflow = =0) Layer[u]=0; returnUflow;}intDinic (intStartintEnd) {    intMaxflow =0;  while(BFS (start, End) = =true) Maxflow+=DFS (Start, oo, End); returnMaxflow;}intMain () {intT; scanf ("%d", &T);  while(t--)    {        intI, F, u, v; scanf ("%d%d%d", &n, &m, &F);        InIt ();  for(i=1; i<=m; i++) scanf ("%d%d", &girl[i], &Boy[i]);  for(i=1; i<=f; i++)        {///combine a friend relationship with a collectionscanf"%d%d", &u, &v); U=Find (U); V=Find (v); if(U! =v) father[u]=v; }         for(i=1; i<=m; i++)        {///The same friend's boyfriend all connected to the root node, boys of the interval n~2*nU =Find (Girl[i]); Love[u].push_back (Boy[i]+N); }        intstart=n*2+1, End = start+1; intleft =0, right = N, ans=0;  while(Left <=Right ) {            intMid = (left+right) >>1;            Buidgraph (Mid, start, End); intMaxflow =dinic (Start, End); if(Maxflow = = mid*N) { left= Mid +1; Ans=Mid; }            Else Right= Mid-1; } printf ("%d\n", ans); }    return 0;}

N-marriage Match ii-hdu 3081 (Max Stream)

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.