根据二分图的性质,最小结点覆盖=结点数-最大匹配数.

Code
1
#include <iostream>
2
using namespace std;
3
4
const int M = 121;
5
int rlink[M];
6
bool g[M][M];
7
bool visited[M];
8
9
int intersections, streets;
10
11
bool Search(int pos)
12

{
13
for(int i = 1; i <= intersections; ++i)
14
{
15
if(!visited[i] && g[pos][i])
16
{
17
visited[i] = true;
18
if(rlink[i] == -1 || Search(rlink[i]))
19
{
20
rlink[i] = pos;
21
return true;
22
}
23
}
24
}
25
26
return false;
27
}
28
29
int _tmain(int argc, _TCHAR* argv[])
30

{
31
int cases;
32
cin >> cases;
33
while(cases--)
34
{
35
cin >> intersections >> streets;
36
37
memset(g, false, sizeof(g));
38
memset(rlink, -1, sizeof(rlink));
39
40
int start, end;
41
for(int i = 0; i < streets; ++i)
42
{
43
cin >> start >> end;
44
g[start][end] = true;
45
}
46
47
int result = 0;
48
49
for(int i = 1; i <= intersections; ++i)
50
{
51
memset(visited, false, sizeof(visited));
52
53
if(Search(i))
54
++result;
55
}
56
57
cout << intersections - result << endl;
58
}
59
return 0;
60
}
61
62
posted on 2009-03-31 21:07
肖羽思 阅读(609)
评论(1) 编辑 收藏 引用 所属分类:
ZOJ