Shirlies
宁静专注认真的程序媛~

有时候都不太想写博客了,(~ o ~)~zZ

这一题是纯粹的二分图匹配,不过要注意一点哈“At the beginning they are both work at mode_0”,所以要判断一下两点连接的时候是不是存在0这个点

if(a && b)
   pro[a].push_back(b);

代码如下:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <vector>
 4 using namespace std;
 5 
 6 int vis[200];
 7 int link[200];
 8 vector<int> pro[200];
 9 int n,m,k;
10 
11 void init()
12 {
13     memset(link,-1,sizeof(link));
14     for(int i = 0;i < n;i ++)
15     {
16         pro[i].clear();
17     }
18 }
19 
20 bool can(int x)
21 {
22     int len = pro[x].size();
23     for(int i = 0;i < len;i ++)
24     {
25         int t = pro[x].at(i);
26         if(!vis[t])
27         {
28             vis[t] = 1;
29             if(link[t] == -1 || can(link[t]))
30             {
31                 link[t] = x;
32                 return true;
33             }
34         }
35     }
36 
37     return false;
38 }
39 
40 int maxmatch()
41 {
42     int num = 0;
43     for(int i = 0;i < n;i ++)
44     {
45         memset(vis,0,sizeof(vis));
46         if(can(i))
47         {
48             num ++;
49         }
50     }
51 
52     return num;
53 }
54 
55 int main()
56 {
57     while(scanf("%d",&n),n)
58     {
59         scanf("%d%d",&m,&k);
60 
61         init();
62         for(int i = 0;i < k;i ++)
63         {
64             int index,a,b;
65             scanf("%d%d%d",&index,&a,&b);
66             if(a && b)
67             pro[a].push_back(b);
68         }
69 
70         printf("%d\n",maxmatch());
71     }
72 
73     return 0;
74 }

 

posted on 2012-05-10 13:19  Shirlies  阅读(215)  评论(0编辑  收藏  举报