using System;
using System.Collections.Generic;
using System.Linq;
// Q057 2部マッチング https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_7_A&lang=jp
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3 4 6");
WillReturn.Add("0 0");
WillReturn.Add("0 2");
WillReturn.Add("0 3");
WillReturn.Add("1 1");
WillReturn.Add("2 1");
WillReturn.Add("2 3");
//3
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int mX; // Xの頂点数
static int mY; // Yの頂点数
static int mSourceNode;
static int mSinkNode;
static int UB;
// 隣接行列で枝を表現
static int[,] mCapacityArr;
static int[,] mFlowArr;
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(X => int.Parse(X)).ToArray();
SplitAct(InputList[0]);
mX = wkArr[0];
mY = wkArr[1];
mSourceNode = mX + mY;
mSinkNode = mSourceNode + 1;
UB = mSinkNode;
mCapacityArr = new int[UB + 1, UB + 1];
mFlowArr = new int[UB + 1, UB + 1];
// グラフに枝を追加する
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
mCapacityArr[wkArr[0], mX + wkArr[1]] = 1;
}
for (int I = 0; I <= mX - 1; I++) {
mCapacityArr[mSourceNode, I] = 1;
}
for (int I = mX; I <= mX + mY - 1; I++) {
mCapacityArr[I, mSinkNode] = 1;
}
// エドモンズ・カープで解く
Solve();
}
static void Solve()
{
while (true) {
List<int> NodeList = ExecBFS();
if (NodeList == null) break;
//Console.WriteLine("経路を発見しました");
//NodeList.ForEach(pX => Console.Write("{0},", pX));
//Console.WriteLine();
// 経路に流す量
int CurrFlow = int.MaxValue;
for (int I = 0; I <= NodeList.Count - 2; I++) {
int FromNode = NodeList[I];
int ToNode = NodeList[I + 1];
CurrFlow = Math.Min(CurrFlow, mCapacityArr[FromNode, ToNode]);
}
//Console.WriteLine("この経路に{0}の水を流します", CurrFlow);
for (int I = 0; I <= NodeList.Count - 2; I++) {
int FromNode = NodeList[I];
int ToNode = NodeList[I + 1];
mCapacityArr[FromNode, ToNode] -= CurrFlow;
mFlowArr[FromNode, ToNode] += CurrFlow;
// 逆辺を追加する
mCapacityArr[ToNode, FromNode] += CurrFlow;
}
}
int Answer = 0;
for (int I = 0; I <= UB; I++) {
Answer += mFlowArr[I, UB];
}
Console.WriteLine(Answer);
}
struct JyoutaiDef
{
internal int CurrNode;
internal List<int> NodeList;
}
// 幅優先探索を行い、始点から終点へのノードのListを返す
// なければnullを返す
static List<int> ExecBFS()
{
var Que = new Queue<JyoutaiDef>();
JyoutaiDef WillEnqueue;
WillEnqueue.CurrNode = mSourceNode; // 始点のノードはmSourceNode
WillEnqueue.NodeList = new List<int>();
WillEnqueue.NodeList.Add(WillEnqueue.CurrNode);
Que.Enqueue(WillEnqueue);
// BFSを繰り返すので、レベルの低い訪問を優先しても問題ない
var VisitedSet = new HashSet<int>();
while (Que.Count > 0) {
JyoutaiDef Dequeued = Que.Dequeue();
// 終点のノードはmSinkNode
if (Dequeued.CurrNode == mSinkNode) {
return Dequeued.NodeList;
}
for (int I = 0; I <= UB; I++) {
int CurrCapacity = mCapacityArr[Dequeued.CurrNode, I];
if (CurrCapacity == 0) continue;
if (VisitedSet.Add(I) == false) continue;
WillEnqueue.CurrNode = I;
WillEnqueue.NodeList = new List<int>(Dequeued.NodeList) { I };
Que.Enqueue(WillEnqueue);
}
}
return null;
}
}