hdu 2063 过山车(二分图匹配)

import java.io.BufferedInputStream;
import java.util.*;
public class Main {

	private int k,m,n;
	private boolean[][] mar;
	private int[] p=new int[505];
	private boolean[] v=new boolean[505];
	private static Scanner sc=new Scanner(new BufferedInputStream(System.in));
	
	boolean findSet(int x)
	{
		for(int i=1;i<=n;i++)
		{
			if(!mar[x][i]||v[i])
				continue;
			v[i]=true;
			if(p[i]==0||findSet(p[i]))
			{
				p[i]=x;
				return true;
			}
		}
		return false;
	}
	void makeSet()
	{
		for(int i=0;i<k;i++)
		{
			int t1=sc.nextInt(),t2=sc.nextInt();
			mar[t1][t2]=true;
		}
		
		int count=0;
		for(int i=1;i<=m;i++)
		{
			Arrays.fill(v, false);
			if(findSet(i))
				count++;
		}
		System.out.println(count);
	}
	void init()
	{
		while(sc.hasNext())
		{
			k=sc.nextInt();
			if(k==0) break;
			
			m=sc.nextInt();
			n=sc.nextInt();
			
			mar=new boolean[505][505];
			Arrays.fill(p, 0);
			makeSet();
		}
	}
	public static void main(String[] args)
	{
		new Main().init();
	}
}

你可能感兴趣的:(hdu 2063 过山车(二分图匹配))