首页 > 代码库 > hdu1151 Air Raid 基础匈牙利

hdu1151 Air Raid 基础匈牙利

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 #define MAXN 150
 8 int n;    //十字路口的数量
 9 int m;    //路的个数
10 int map[MAXN][MAXN];
11 int x[MAXN], y[MAXN];
12 int mark[MAXN];
13 
14 int search(int a)
15 {
16     for (int i = 1; i <= n; i++)
17     {
18         if (map[a][i] && !mark[i])
19         {
20             mark[i] = 1;
21             if (y[i] == 0 || search(y[i]))
22             {
23                 x[a] = i;
24                 y[i] = a;
25                 return a;
26             }
27         }
28     }
29     return 0;
30 }
31 
32 int main()
33 {
34     int t;
35     scanf("%d", &t);
36     while (t--)
37     {
38         memset(map, 0, sizeof(map));
39         memset(mark, 0, sizeof(mark));
40         scanf("%d %d", &n, &m);
41         for (int i = 0; i < m; i++)
42         {
43             int a, b;
44             scanf("%d %d", &a, &b);
45             if (map[a][b] == 0)
46                 map[a][b] = 1;
47         }
48 
49         int ans1 = 0;
50         memset(x, 0, sizeof(x));
51         memset(y, 0, sizeof(y));
52 
53         for (int i = 1; i <= n; i++)
54         {
55             if (x[i] == 0)
56             {
57                 memset(mark, 0, sizeof(mark));
58                 if (search(i))
59                     ans1++;
60             }
61         }
62         printf("%d\n", n - ans1);
63     }
64     //system("pause");
65     return 0;
66 }

 

hdu1151 Air Raid 基础匈牙利