hdu1151 Air Raid

http://acm.hdu.edu.cn/showproblem.php?pid=1151

增广路的变种2:DAG图的最小路径覆盖=定点数-最大匹配数

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #include<math.h>
 6 using namespace std;
 7 const int N=510;
 8 int a[N][N];
 9 int use[N];
10 int match[N];
11 int n;
12 int dfs(int u)
13 {
14     for(int i=1;i<=n;i++)
15     {
16         if(a[u][i] && !use[i])//判断是否可匹配,以及是否使用
17         {
18             use[i]=1;//标记使用
19             if(match[i]==-1 || dfs(match[i]))//未匹配,直接进行匹配
20             {                                //已匹配,将原匹配进行拆分,去选择其他可匹配的选项
21                 match[i]=u;                  //再将当前进行强制匹配
22                 return 1;
23             }
24         }
25     }
26     return 0;
27 }
28 int bipartite()
29 {
30     int res=0;
31     memset(match,-1,sizeof(match));
32     for(int i=1;i<=n;i++)
33     {
34         memset(use,0,sizeof(use));
35         if(dfs(i))
36         res++;
37     }
38     return res;
39 }
40 int main()
41 {
42     //freopen("in.txt","r",stdin);
43     int t;
44     scanf("%d",&t);
45     while(t--)
46     {
47         scanf("%d",&n);
48         memset(a,0,sizeof(a));
49         int m,q,p;
50         scanf("%d",&m);
51         for(int i=0;i<m;i++)
52         {
53             scanf("%d%d",&q,&p);
54             a[q][p]=1;
55         }
56         int x=n-bipartite();
57         printf("%d
",x);
58     }
59     return 0;
60 }
原文地址:https://www.cnblogs.com/xuesen1995/p/4537183.html