[POJ] [1466] Girls and Boys

Problem Description

Loading...

Problem Analysis

There are quite a few methods
I used Bipartite Graph matching

There was a hiccup: I wrote the program’s termination condition wrong
so it kept timing out…
I even thought it was a problem with the algorithm itself…

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<stdio.h>
#include<string.h>
int line[520][520];
int used[520],flag[520];
int n;
bool find(int x) {
	for(int i=0; i<n; i++) {
		if(!used[i]&&line[x][i]) {
			used[i]=1;
			if(flag[i]==-1||find(flag[i])) {
				flag[i]=x;
				return true;
			}
		}
	}
	return false;
}
int main() {
	while(scanf("%d",&n)!=EOF) {
		memset(line,0,sizeof(line));
		memset(flag,-1,sizeof(flag));
		for(int i=0; i<n; i++) {
			int t,k;
			scanf("%d: (%d)",&t,&k);
			while(k--) {
				int ty;
				scanf("%d",&ty);
				line[t][ty]=1;
			}
		}
		int cnt=0;
		for(int i=0; i<n; i++) {
			memset(used,0,sizeof(used));
			if(find(i)) {
				cnt++;
			}
		}
		printf("%d\n",n-cnt/2);
	}
	return 0;
}

Problem link:【POJ】[1466]Girls and Boys

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...