洛谷上面有一整套题。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 extra

①飞行员配对方案问题。top

裸二分图匹配。

 1 #include <cstdio>
 2 
 3 const int N = 110, M = 10010;
 4 
 5 struct Edge {
 6     int nex, v;
 7 }edge[M << 1]; int top;
 8 
 9 int n, m, e[N], mat[N], vis[N], Time;
10 
11 inline void add(int x, int y) {
12     top++;
13     edge[top].v = y;
14     edge[top].nex = e[x];
15     e[x] = top;
16     return;
17 }
18 
19 bool DFS(int x) {
20     for(int i = e[x]; i; i = edge[i].nex) {
21         int y = edge[i].v;
22         if(vis[y] == Time) {
23             continue;
24         }
25         vis[y] = Time;
26         if(!mat[y] || DFS(mat[y])) {
27             mat[y] = x;
28             return 1;
29         }
30     }
31     return 0;
32 }
33 
34 int main() {
35     scanf("%d%d", &m, &n);
36     int x, y;
37     while(scanf("%d%d", &x, &y)) {
38         if(x == -1 && y == -1) {
39             break;
40         }
41         add(x, y);
42         add(y, x);
43     }
44 
45     int ans = 0;
46     for(int i = 1; i <= m; i++) {
47         Time = i;
48         if(DFS(i)) {
49             ans++;
50         }
51     }
52 
53     if(!ans) {
54         printf("No Solution!");
55         return 0;
56     }
57 
58     printf("%d\n", ans);
59     for(int i = m + 1; i <= n; i++) {
60         if(mat[i]) {
61             printf("%d %d", mat[i], i);
62             ans--;
63             if(ans) {
64                 puts("");
65             }
66         }
67     }
68 
69     return 0;
70 }
匈牙利AC代码

相关文章:

  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-09
  • 2021-07-24
  • 2021-10-14
  • 2021-10-22
相关资源
相似解决方案