bzoj1305 "CQOI2009" Dance Dance

Source: Internet
Author: User

1305: [Cqoi2009]dance Dance Time limit: 5 Sec Memory Limit: 162 MB
Submit: 2041 Solved: 853
[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




Maximum flow + Two-point answer

Divide everyone I into three points, i1 (total points), i2 (like), i3 (dislike).

For each pair of boys I and Girl J, if each other like so the connecting edge (i2,j2,1), otherwise the Edge (i3,j3,1). Because each boy and the same girl Can dance up to 1 dances.

For each boy I, the Edge (I1,i2,inf) (I1,i3,inf). Girls. Because there is no limit to the number of people who dance with the person they like, there is a limited number of dances with people they don't like.

Finally, we have the two-point answer, set the current detection value is x. For each boy I, the Edge (s,i1,x). Girls. Then run the maximum flow to detect whether the stream is full.

Note that you need to re-compose every two minutes.




#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include < cstdlib> #include <cstring> #include <queue> #define F (i,j,n) for (int i=j;i<=n;i++) #define D (I,j,n) for (int i=j;i>=n;i--) #define LL long long#define pa pair<int,int> #define MAXN 500#define maxm 5000#define INF 100 0000000#define F1 (x) (x*3-2) #define F2 (x) (n*3+x*3-2) using namespace Std;int n,k,cnt,s,t,ans,l,r,mid,dis[maxn],head[ Maxn],cur[maxn];char ch[100];bool f[100][100];struct edge_type{int next,to,v;} e[maxm];inline void Add_edge (int x,int y,int v) {e[++cnt]= (Edge_type) {head[x],y,v};head[x]=cnt;e[++cnt]= (Edge_type) { head[y],x,0};head[y]=cnt;} inline bool BFs () {queue<int>q;memset (dis,-1,sizeof (dis));d Is[s]=0;q.push (s), while (!q.empty ()) {int tmp= Q.front (); Q.pop (); if (tmp==t) return true;for (int i=head[tmp];i;i=e[i].next) if (e[i].v&&dis[e[i].to]==-1) { Dis[e[i].to]=dis[tmp]+1;q.push (e[i].to);}} return false;} inline int dfs (int x,int f) {if (x==t) return F; int tmp,sum=0;for (int &i=cur[x];i;i=e[i].next) {int y=e[i].to;if (e[i].v&&dis[y]==dis[x]+1) {Tmp=dfs (Y, Min (f-sum,e[i].v)); Sum+=tmp;e[i].v-=tmp;e[i^1].v+=tmp;if (sum==f) return sum;}} if (!sum) Dis[x]=-1;return sum;} inline void Dinic () {Ans=0;while (BFS ()) {F (i,1,t) Cur[i]=head[i];ans+=dfs (S,inf);}} inline void build (int x) {Cnt=1;memset (head,0,sizeof (head)); F (i,1,n) {Add_edge (S,f1 (i), x), Add_edge (F2 (i), t,x), Add_edge (F1 (i), F1 (i) +1,inf), Add_edge (F2 (i) +1,f2 (i), INF);} if (k) F (i,1,n) Add_edge (F1 (i), F1 (i) +2,k), Add_edge (F2 (i) +2,f2 (i), k); F (i,1,n) F (j,1,n) {if (F[i][j]) Add_edge (F1 (i) +1,f2 (j) +1,1); else Add_edge (F1 (i) +2,f2 (j) +2,1);}} int main () {scanf ("%d%d", &n,&k); s=n*6+1;t=s+1; F (i,1,n) {scanf ("%s", ch); F (j,1,n) f[i][j]= (ch[j-1]== ' Y ');} L=0;r=n;while (l<r) {mid= (l+r+1) >>1;build (mid);d inic (); if (ans==mid*n) L=mid;else r=mid-1;} printf ("%d\n", L);}


bzoj1305 "CQOI2009" Dance Dance

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.