觉得浮夸了四年,漠然发现原来是浮躁了四年!

hdu 二分图最大匹配问题 (hdu 1083)

给定一个二分图G,M为G边集的一个子集,如果M满足当中的任意两条边都不依附于同一个顶点,则称M是一个匹配

最大匹配即最大的匹配个数。

关于二分匹配的基本问题:http://www.cnblogs.com/heat-man/archive/2013/03/26/2982644.html

hdu 1083   courses(http://acm.hdu.edu.cn/showproblem.php?pid=1083

本题题目叙述有点乱,英语不好看了很久,有p门课,n个学生,每个学生可以选修1-p门课,给出了每一门课的学生选择信息(学生的序号),现在要成一个委员会,需满足每一门课都有一个选修了该课程的学生来做代表,每个学生只能代表一门课程,问能否组成委员会,如果能,则输出“YES”,否则“NO”;

二分图的最大匹配问题,以课程-学生建立二分图,求出最大匹配数等于课程数即满足。

View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 #define N 305
 6 int match[N][N];
 7 int mark[N];
 8 int visted[N];
 9 int n,p;
10 int t ;
11 int DFS(int x)
12 {
13     int j;
14     for(j=1;j<=n;j++)
15     {
16         if(!visted[j]&&match[x][j]==1)
17         {
18             visted[j]=1;
19             if(!mark[j]||DFS(mark[j]))
20             {
21                 mark[j]=x;
22                 return 1;
23             }
24         }
25     }
26     return 0;
27 }
28 int main()
29 {
30     //freopen("input.txt","r",stdin);
31       // freopen("output.txt","w",stdout);
32     scanf("%d",&t);
33     while(t--)
34     {
35 
36         memset(match,0,sizeof(match));
37         memset(mark,0,sizeof(mark));
38         scanf("%d%d",&p,&n);
39         if(p>n)
40         {
41             printf("NO\n");
42             continue ;
43         }
44         int i;
45         for(i=1;i<=p;i++)
46         {
47             int num;
48             int j;
49             scanf("%d",&num);
50             for(j=1;j<=num;j++)
51             {
52                 int a;
53                 scanf("%d",&a);
54                 match[i][a]=1;
55 
56             }
57         }
58         int  ans=0;
59         for(i=1;i<=p;i++)
60         {
61             memset(visted,0,sizeof(visted));
62             if(DFS(i))
63                 ans++;
64         }
65         if(ans==p)
66             printf("YES\n");
67         else
68             printf("NO\n");
69     }
70     return 0;
71 }

 

posted @ 2013-05-02 20:03  heat nan  阅读(312)  评论(0)    收藏  举报