Bzoj 1305: [Cqoi2009]dance dancing (Max stream)

Source: Internet
Author: User

Cloud God code is very short ... 0 ms over the ... Look at the code, probably greedy ... orz I can't testify.

The data is so small to mess it up ... ←_←

This problem network flow can still write ...

since the limit can only and K not like the people dance, then put everyone into a, b two points. After the limit is over. , because n <= 50, we can directly from small to large enumeration, an augmentation.

Detailed map:

Boy_a (i), Boy_b (i) (cap:k), Girl_b (i), girl_a (i) (cap:k) (1 <= i <= N)

For two men Boy (i), Girl (j):

Boy (i) and Girl (j) is friends:boy_a (i), Girl_a (j) (1)

Boy (i) and girl (j) aren ' t friends:boy_b (i), Girl_b (j) (1)

Then each time to the S-boy_a (i), girl_a (i)--T (1 <= i <= N) plus a 1 arc, run to see if the increased traffic is n, if it is to continue and answer + 1, not the end

-------------------------------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#define REP (i, n) for (int i = 0; i < n; ++i)#define CLR (x, C) memset (x, C, sizeof (x))#define REP (i, n) for (int i = 1; I <= n; ++i)using namespace std;const int MAXN = ten +;const int MAXM = 20000;const int inf = 1E8;struct Edge {int to, cap;Edge *next, *rev;} *pt, *head[MAXN], e[MAXM];inline void Add (int u, int v, int d) {pt-to = V;pt, cap = D;pt-Next = head[u];head[U] = pt++;}inline void Add_edge (int u, int v, int d) {Add (U, V, d);Add (V, u, 0);head[u], rev = head[V];Head[v], rev = head[U];}void Edge_init () {pt = E;CLR (head, 0);}int h[MAXN], cnt[MAXN];Edge *p[MAXN], *cur[MAXN];int N, S, T; int Maxflow () {CLR (CNT, 0), cnt[0] = N;CLR (h, 0);Rep (i, N) cur[i] = head[i];int A = inf, x = S, flow = 0;edge* E;While (h[S] < N) {For (E = cur[x]; e; e = e = next)if (Cap && h[x] = = h[E--] + 1) break;if (e) {p[e-to] = cur[x] = e;a = min (A, e-cap);x = e -to;if (x = = T) {For (; x! = S; x = p[x], rev-to) {p[x], caps-= A;p[X]-rev, Cap + A;}flow + = A;A = inf;}} else {if (!--cnt[h[X]]) break;h[x] = N;For (E = head[x]; e; e = e = next)if (Cap && h[E, to] + 1 < h[x]) {h[x] = h[E to] + 1;cur[x] = e;    }++cnt[h[x]];if (x! = S) x = p[x], rev to;}}return flow;}#define BOY_A (i) (i)#define GIRL_A (i) (i + (n << 1))#define BOY_B (i) (i + N)#define GIRL_B (i) (i + N * 3)int main () {freopen ("test.in", "R", stdin);freopen ("Test.out", "w", stdout);edge_init ();int n, K;char c;cin >> n >> K;S = 0, t = 4 * n + 1, n = T + 1;Rep (i, n) {Add_edge (Boy_a (i), boy_b (i), k);Add_edge (Girl_b (i), girl_a (i), k);}Rep (i, N)Rep (j, N) {scanf ("%c", &c);if (c = = ' Y ')Add_edge (Boy_a (i), Girl_a (j), 1);ElseAdd_edge (Boy_b (i), Girl_b (j), 1);    }int ans = 0;for (;;) {Rep (i, n) {Add_edge (S, Boy_a (i), 1);Add_edge (Girl_a (i), T, 1);}if (maxflow () = = n) ans++;else break;}cout << ans << "\ n";return 0;}

-------------------------------------------------------------------------------------------------

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

Bzoj 1305: [Cqoi2009]dance dancing (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.