hdu1151 Air Raid

来源:互联网 发布:win7装mac os双系统 编辑:程序博客网 时间:2024/06/09 00:51
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\n",x);58     }59     return 0;60 }