Roller coaster time limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 11477 accepted submission (s): 5051
Problem descriptionrpg girls went to the playground today and finally got on the coveted roller coaster. However, there are only two seats in each row of the roller coaster, and there is an unwritten rule, that is, every girl must find a boy as a partner and sit with her. However, every girl has her own ideas. For example, rabbit is only willing to be a partner with xhd or pqk. Grass is only willing to be a partner with linle or ll, princesssnow is willing to be a partner with the water prodigal son or pseudo cool. Considering the funding issue, boss Liu decided to let the partner find a ride on the roller coaster. Other people, hey, just stand and watch it. Smart acmer, can you help calculate how many combinations can ride on a roller coaster?
The first line of input data is a three integer K, M, N, indicating the number of possible combinations, the number of girls, and the number of boys. 0 <k <= 1000
1 <= N and M <= 500. In the next K rows, each row has two numbers, indicating that female AI is willing to be partner with boys' BJ. The last 0 end input.
Output outputs an integer for each group of data, indicating the maximum number of combinations that can be used on a roller coaster.
Sample Input
6 3 31 11 21 32 12 33 10
Sample output
3
Authorprincesssnow
Sourcerpg exercise session
Hungary template question.
#include <stdio.h>#include <string.h>#define maxn 510#define maxm 1010int k, m, n;int B[maxn];bool vis[maxn];int head[maxn], id;struct Node {int v, next;} E[maxm];void addEdge(int u, int v) {E[id].v = v; E[id].next = head[u];head[u] = id++;}void getMap() {int u, v; id = 0;memset(head, -1, sizeof(int) * (m + 1));while(k--) {scanf("%d%d", &u, &v);addEdge(u, v);}}int findPath(int k) {int i, v;for(i = head[k]; i != -1; i = E[i].next) {if(!vis[v=E[i].v]) {vis[v] = 1;if(B[v] == -1 || findPath(B[v])) {B[v] = k; return 1;}}}return 0;}int MaxMatch() {memset(B, -1, sizeof(int) * (n + 1));int i, ans = 0;for(i = 1; i <= m; ++i) {memset(vis, 0, sizeof(vis));ans += findPath(i);}return ans;}void solve() {printf("%d\n", MaxMatch());}int main() {// freopen("stdin.txt", "r", stdin);while(scanf("%d%d%d", &k, &m, &n) == 3) {getMap();solve();}return 0;}
Hdu2063 roller coaster [bipartite graph · maximum matching]