Problem:
Problem G: Make Friendships

Isaac H. Ives attended an international student party and made a lot of girl friends (as many other persons
expected). To strike up a good friendship with them, he decided to have dates with them. However, it
is hard for him to schedule dates because he made so many friends. Thus he decided to find the best
schedule using a computer program. The most important criterion in scheduling is how many different
girl friends he will date. Of course, the more friends he will date, the better the schedule is. However,
though he has the ability to write a program finding the best schedule, he doesn’t have enough time to
write it.

Your task is to write a program to find the best schedule instead of him.

Input

The input consists of a series of data sets. The first line of each data set contains a single positive integer
N (N ≤ 1,000) that represents the number of persons Isaac made friends with in the party. The next line
gives Isaac’s schedule, followed by N lines that give his new friends’ schedules. Each schedule consists of
a positive integer M that represents the number of available days followed by M positive integers each
of which represents an available day.

The input is terminated by a line that contains a single zero. This is not part of data sets and should
not be processed.

Output

For each data set, print a line that contains the maximum number of girl friends Isaac can have dates
with.

Sample Input

3
3 1 3 5
2 1 4
4 1 2 3 6
1 3
0

Output for the Sample Input

2

Solution in C (judge verdict: Accepted)

// AOJ 2064: Make Friendships
// 2017.11.8 bal4u@uu

#include <stdio.h>
#include <string.h>

#define HASHSIZ 2003
typedef struct { int v, id; } HASH;
HASH hash[HASHSIZ+2], *hashend = hash+HASHSIZ;

int lookup(int v)
{
	HASH *p = hash + v % HASHSIZ;
	while (p->v) {
		if (p->v == v) return p->id;
		if (++p == hashend) p = hash;
	}
	return -1;
}

void insert(int v, int id)
{
	HASH *p = hash + v % HASHSIZ;
	while (p->v) {
		if (p->v == v) return;
		if (++p == hashend) p = hash;
	}
	p->v = v, p->id = id;
}

#define MAX 1002
int   m, n;
char  graph[MAX][MAX];
char  seen[MAX];
short matchL[MAX], matchR[MAX];

int bpm(int u)
{
  int v;

  for (v = 0; v < n; v++) if (graph[u][v]) {
    if (seen[v]) continue;
    seen[v] = 1;
    if (matchR[v] < 0 || bpm(matchR[v])) {
      matchL[u] = v;
      matchR[v] = u;
      return 1;
    }
  }
  return 0;
}

int main()
{
	int M, i, j, ans;

	while (scanf("%d", &n) && n > 0) {
		memset(hash, 0, sizeof(hash));
		memset(graph, 0, sizeof(graph));
		scanf("%d", &M);
		m = 0; while (M--) {
			scanf("%d", &j);
			insert(j, m++);
		}
		for (i = 0; i < n; i++) {
			scanf("%d", &M);
			while (M--) {
				scanf("%d", &j);
				if ((j = lookup(j)) >= 0) graph[j][i] = 1;
			}
		}

	    memset(matchL, -1, m*sizeof(matchL[0]));
		memset(matchR, -1, n*sizeof(matchR[0]));

		ans = 0;
		for (i = 0; i < m; i++) {
			memset(seen, 0, n);
			if (bpm(i)) ans++;
		}
		printf("%d\n", ans);
	}
	return 0;
}