hdu 1150 Machine Schedule (最小点覆盖)

hdu 1150 Machine Schedule (最小点覆盖)

结论:二分图的最小点覆盖数=最大匹配数

import java.io.*;
import java.util.*;
import java.math.*;

class Edge {
	int t , next ;
}

class solution {
	static Scanner in = new Scanner ( System.in ) ;
	static PrintWriter out = new PrintWriter ( System.out ) ;
	int n , m ;
	int[] c = new int[1111] ;
	Edge[] edge = new Edge[1111] ;
	int tot ;
	int[] head = new int[1111] ;
	int[] vis = new int[1111] ;
	void init () {
		tot = 0 ;
		for ( int i = 0 ; i < 1111 ; i ++ ) c[i] = head[i] = -1 ;
	}
	void new_edge ( int a , int b ) {
		edge[tot] = new Edge () ;
		edge[tot].t = b ;
		edge[tot].next = head[a] ;
		head[a] = tot ;
		tot ++ ;
	}
	int dfs ( int u ) {
		int i ;
		for ( i = head[u] ; i != -1 ; i = edge[i].next ) {
			int e = edge[i].t ;
			if ( vis[e] == 1 ) continue ;
			vis[e] = 1 ;
			if ( c[e] == -1 || dfs ( c[e] ) == 1 ) {
				c[e] = u ;
				return 1 ;
			}
		}
		return 0 ;
	}
	void solve () {
		int i , k , j ;
		while ( in.hasNext () ) {
			init () ;
			n = in.nextInt() ;
			if ( n == 0 ) break ;
			m = in.nextInt() ; k = in.nextInt() ;
			for ( i = 1 ; i <= k ; i ++ ) {
				int a , b , c ;
				a = in.nextInt() ; b = in.nextInt() ; c = in.nextInt() ;
				new_edge ( b ,c ) ;
			}
			int ans = 0 ;
			for ( i = 1 ; i <= n ; i ++ ) {
				for ( j = 1 ; j <= m ; j ++ ) vis[j] = 0 ;
				ans += dfs ( i ) ;
			}
			out.println ( ans ) ;
		}
		out.close () ;
	}
}

public class Main {
	static public void main ( String[] args ) {
		solution fuck = new solution () ;
		fuck.solve () ;
	}
}


你可能感兴趣的:(二分匹配)