[bzoj]1305: [Cqoi2009]dance dancing two points + network stream

Source: Internet
Author: User

Description

There were n boys and n Girls at a dance. At the beginning of each song, all the boys and girls fit into the N-dance ballroom. Every boy will not dance with the same girl for two (or more) dance songs. There are some boys and girls who like each other, while others dislike each other (not "one-way Likes"). Each boy is willing to dance with the K-not-like girls at most, and each girl is willing to dance with the K-not-liked boys at most. Given the information about whether each pair of boys and girls like each other, the ball can have a few dance songs. The following:

Simple two-point + network flow, a problem of water, two points can have a few dance, and then use the network flow to judge, directly in accordance with the test instructions edge, and the most and k do not like the dance of this condition, I was so dealt with, each boy girl split into two points, an expression and it likes to dance, an expression and it does not like the dance. Code:

#include <bits/stdc++.h> using namespace std;
#define LL Long Long const int inf=2147483647;
const int maxn=60;
    int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
    while (ch>= ' 0 ' &&ch<= ' 9 ') {x= (x<<3) + (x<<1) +ch-' 0 '; Ch=getchar ();}
return x*f; } struct Edge{int y,d,next;}
E[500000];
int Last[maxn*4],len;
    void ins (int x,int y,int d) {int t=++len;
    E[t].y=y;e[t].d=d;
e[t].next=last[x];last[x]=t;
} void Addedge (int x,int y,int D) {ins (x,y,d); ins (y,x,0);} int n,k;
BOOL In[maxn*4];
queue<int>q;
int h[maxn*4],st,ed;
    BOOL BFs () {memset (h,0,sizeof (h)); h[st]=1;
    Q.push (ST);
        while (!q.empty ()) {int X=q.front (); Q.pop ();
    for (int i=last[x];i;i=e[i].next) if (E[i].d&&!h[e[i].y]) H[e[i].y]=h[x]+1,q.push (E[I].Y);
} return h[ed];
    } int dfs (int x,int f) {if (x==ed) return F;
    int s=0,t; for (int i=last[x];i;i=e[i].next) {iNT Y=E[I].Y;
            if (h[y]==h[x]+1&&e[i].d&&s<f) {T=dfs (Y,min (F-S,E[I].D));
        s+=t;e[i^1].d+=t;e[i].d-=t;
    }} if (s==0) h[x]=0;
return s;
} bool LIKE[MAXN][MAXN];
    Boys 1~n Boys 2 n+1~2n schoolgirl 2n+1~3n schoolgirl 2 3n+1~4n bool Check (int x) {if (x==0) return true;
    Memset (Last,0,sizeof (last)); len=1;
    st=4*n+1;ed=4*n+2;
        for (int i=1;i<=n;i++) {Addedge (st,i,x);
        Addedge (I,I+N,K);
        for (int j=1;j<=n;j++) if (Like[i][j]) Addedge (i,j+2*n,1);
    else Addedge (i+n,j+3*n,1);
        } for (int i=1;i<=n;i++) {Addedge (i+2*n,ed,x);
    Addedge (I+3*N,I+2*N,K);
    } int ans=0;
    while (BFS ())) Ans+=dfs (St,inf);
return ans==n*x;
    } int main () {n=read (); K=read ();
        for (int i=1;i<=n;i++) {char STR[MAXN];
        scanf ("%s", str+1);
    for (int j=1;j<=n;j++) if (str[j]== ' Y ') like[i][j]=true;
    } int l=0,r=n;
  while (L&LT;=R)  {int mid=l+r>>1;
        if (check (mid)) l=mid+1;
    else r=mid-1;
} printf ("%d", l-1); }

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.