USACO 4.2 The Perfect Stall(匈牙利)

二分图最大匹配模版题。

 1 /*

 2  ID: cuizhe

 3  LANG: C++

 4  TASK: stall4

 5 */

 6 #include <cstdio>

 7 #include <cstring>

 8 #include <cmath>

 9 #include <string>

10 #include <ctime>

11 using namespace std;

12 int p[201][201],used[201],linker[201];

13 int n,m;

14 int dfs(int x)

15 {

16     int i;

17     for(i = 1;i <= n;i ++)

18     {

19         if(p[x][i]&&!used[i])

20         {

21             used[i] = 1;

22             if(!linker[i]||dfs(linker[i]))

23             {

24                 linker[i] = x;

25                 return 1;

26             }

27         }

28     }

29     return 0;

30 }

31 int main()

32 {

33     int i,j,num,ev,ans;

34     freopen("stall4.in","r",stdin);

35     freopen("stall4.out","w",stdout);

36     scanf("%d %d",&n,&m);

37     for(i = 1;i <= m;i ++)

38     {

39         scanf("%d",&num);

40         for(j = 1;j <= num;j ++)

41         {

42             scanf("%d",&ev);

43             p[i][ev] = 1;

44         }

45     }

46     ans = 0;

47     for(i = 1;i <= n;i ++)

48     {

49         memset(used,0,sizeof(used));

50         if(dfs(i))

51         ans ++;

52     }

53     printf("%d\n",ans);

54     return 0;

55 }

 

你可能感兴趣的:(USACO)