1305: [Cqoi2009]dance dancing

Source: Internet
Author: User

Time Limit:5 Sec Memory limit:162 MB
submit:4169 solved:1804
[Submit] [Status] [Discuss] 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 that every pair of boys and girls love each other, how many dances can a ball have?

Input

The first line consists of two integers n and K. The following n rows contain n characters per line, where the J character of Line I is ' Y ' when and only if boy I and girl J love each other.

Output

Only one number, that is, the maximum number of dances.

Sample Input3 0
YYY
YYY
YYY
Sample Output3HINT

N<=50 k<=30

Source

Strengthening data by dwellings and LIYIZHEN2

At first glance, think that the multi-binary map matching, spent half an hour to write 82 points WA, search the main points to know is the maximum flow of network flow split

All the people were split into two points, boys for XI,XJ, girls for Yi,yj

The people who will like each other, from Xi to Yi, the capacity of 1

Who will not like each other, from XJ to YJ, with a capacity of 1

Connect all boys ' XI to XJ with a capacity of K

Connect all female YJ to Yi with a capacity of K

Connect the source point to Xi with a capacity of

Yi connects to the meeting point with a capacity of

The dichotomy enumerates the capacity A, calculates the maximum flow, if the flow<n*a, that is, cannot fill the stream, then stops the two points

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5 using namespacestd;6 7 Const intinf=0x7f7f7f7f;8 Const intmaxn=400000;9 Ten structEdge One { A     intTo,w,next; - }E[MAXN]; - intNode=1, HEAD[MAXN],DIS[MAXN]; the ints=0, t= +; - intN,k,ans; - BOOLmp[ -][ -]; -  + voidInsertintUintVintW) - { +e[++node]=(Edge) {v,w,head[u]}; Ahead[u]=node; atE[++node]= (Edge) {u,0, Head[v]}; -head[v]=node; - } -  - BOOLBFS () - { inmemset (dis,-1,sizeof(DIS)); -queue<int>Q; to Q.push (s); +dis[s]=0; -      while(!q.empty ()) the     { *         intq=Q.front (); Q.pop (); $          for(intI=head[q];i;i=e[i].next)Panax Notoginseng             if(e[i].w&&dis[e[i].to]==-1) -             { the Q.push (e[i].to); +dis[e[i].to]=dis[q]+1; A             } the     } +     returndis[t]!=-1; - } $  $ intDfsintXintflow) - { -     if(x==t)returnflow; the     intW,used=0; -      for(intI=head[x];i;i=e[i].next)Wuyi         if(e[i].w&&dis[e[i].to]==dis[x]+1) the         { -w=flow-used; Wuw=Dfs (E[i].to,min (W,E[I].W)); -e[i].w-=W; Aboute[i^1].w+=W; $used+=W; -             if(Used==flow)returnflow; -         } -     if(!used) dis[x]=-1; A     returnused; + } the  - voiddinic () $ { the      while(BFS ()) ans+=DFS (s,inf); the } the  the intMain () - { inscanf"%d%d",&n,&k); the      for(intI=1; i<=n;i++) the     { About         Charch[ -]; thescanf"%s", ch); the          for(intj=1; j<=n;j++) the             if(ch[j-1]=='Y') mp[i][j]=1; +     } -     intleft=0, right= -; the      while(left<=Right )Bayi     { the         intMid= (left+right) >>1; theNode=1; Memset (Head,0,sizeof(head)); -          for(intI=1; i<=n;i++) -         { theInsert0, i,mid); theInsert (i,i+ -, k); theInsert (n+i+ -, n+i,k); theInsert (n+i,t,mid); -              for(intj=1; j<=n;j++) the                 if(Mp[i][j]) insert (I,j+n,1); the                 ElseInsert (i+ -, j+n+ -,1); the         }94ans=0;d inic (); the         if(Ans>=n*mid) left=mid+1; the         Elseright=mid-1; the     }98printf"%d", left-1); About     return 0; -}

1305: [Cqoi2009]dance dancing

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.