Problem Description


RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了。可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐。但是,每个女孩都有各自的想法,举个例子把,Rabbit只愿意和XHD或PQK做partner,Grass只愿意和linle或LL做partner,PrincessSnow愿意和水域浪子或伪酷儿做partner。考虑到经费问题,boss刘决定只让找到partner的人去坐过山车,其他的人,嘿嘿,就站在下面看着吧。聪明的Acmer,你可以帮忙算算最多有多少对组合可以坐上过山车吗?


 



Input


输入数据的第一行是三个整数K , M , N,分别表示可能的组合数目,女生的人数,男生的人数。0<K<=1000
1<=N 和M<=500.接下来的K行,每行有两个数,分别表示女生Ai愿意和男生Bj做partner。最后一个0结束输入。


 



Output


对于每组数据,输出一个整数,表示可以坐上过山车的最多组合数。


 



Sample Input


6 3 3 1 1 1 2 1 3 2 1 2 3 3 1 0


 



Sample Output


3


二分图匹配,可以用网络流求最小割

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 1e5 + 10;
int T, n1, n2, m, x, y;

struct MaxFlow
{
struct Edges
{
int end, flow;
Edges(){}
Edges(int end, int flow) :end(end), flow(flow){}
}edge[maxn];
int first[maxn], next[maxn], dis[maxn], tot;

void clear(){ tot = 0; memset(first, -1, sizeof(first)); }

void AddEdge(int s, int t, int flow)
{
edge[tot] = Edges(t, 0); next[tot] = first[s]; first[s] = tot++;
edge[tot] = Edges(s, flow); next[tot] = first[t]; first[t] = tot++;
}

bool bfs(int s, int t)
{
memset(dis, -1, sizeof(dis));
queue<int> p; p.push(s); dis[s] = 0;
while (!p.empty())
{
int q = p.front(); p.pop();
for (int i = first[q]; i >= 0; i = next[i])
{
if (edge[i ^ 1].flow&&dis[edge[i].end] == -1)
{
p.push(edge[i].end);
dis[edge[i].end] = dis[q] + 1;
}
}
}
return dis[t] != -1;
}

int dfs(int s, int t, int low)
{
if (s == t) return low;
for (int i = first[s]; i >= 0; i = next[i])
{
if (dis[s] + 1 == dis[edge[i].end] && edge[i ^ 1].flow)
{
int x = dfs(edge[i].end, t, min(low, edge[i ^ 1].flow));
if (x)
{
edge[i].flow += x; edge[i ^ 1].flow -= x;
return x;
}
}
}
return 0;
}

int dinic(int s, int t)
{
int maxflow = 0, inc = 0;
while (bfs(s, t)) while (inc = dfs(s, t, 0x7FFFFFFF)) maxflow += inc;
return maxflow;
}
}solve;

int main()
{
//scanf("%d", &T);
while (scanf("%d", &m), m)
{
scanf("%d%d", &n1, &n2);
solve.clear();
for (int i = 1; i <= n1; i++) solve.AddEdge(0, i, 1);
for (int i = 1; i <= n2; i++) solve.AddEdge(n1 + i, n1 + n2 + 1, 1);
while (m--)
{
scanf("%d%d", &x, &y);
solve.AddEdge(x, n1 + y, 1);
}
printf("%d\n", solve.dinic(0, n1 + n2 + 1));
}
return 0;
}