hdu 1151 Air Raid DAG最小边覆盖 最大二分匹配

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151

题目大意:

城镇之间互相有边,但都是单向的,并且不会构成环,现在派伞兵降落去遍历城镇,问最少最少派多少人去

思路:

转化题意,求用最少的有向边覆盖点        -------->      最小边覆盖数目=顶点数-最大二分匹配数目

注意:这道题目中说得是有向无环的DAG,所以顶点数目不能按照2倍来计算

在DAG中我们假设点集为 -----> Ni     然后建立每个点对应的虚点 ------>Ni

那么在二分匹配的时候绝对不会出现  N1---->N2‘   N2---->N1‘ 这样的情况,这样的就构成了环状

所以在DAG中求取最小边覆盖,点数不用加倍

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 int g[125][125],vis[125],who[125];
 4 int n;
 5 bool find(int x) {
 6     for(int i=1; i<=n; ++i) {
 7         if(g[x][i]&&!vis[i]) {
 8             vis[i]=1;
 9             if(!who[i]||find(who[i])) {
10                 who[i]=x;
11                 return true;
12             }
13         }
14     }
15     return false;
16 }
17 int main() {
18     int t;
19     scanf("%d",&t);
20     while(t--) {
21         memset(g,0,sizeof(g));
22         memset(who,0,sizeof(who));
23         int m,u,v;
24         scanf("%d",&n);
25         scanf("%d",&m);
26         while(m--) {
27             scanf("%d %d",&u,&v);
28             g[u][v]=1;
29         }
30         int sum=0;
31         for(int i=1; i<=n; ++i) {
32             memset(vis,0,sizeof(vis));
33             if(find(i)) sum++;
34         }
35         printf("%d\n",n-sum);
36     }
37     return 0;
38 }

时间: 2024-10-11 16:01:39

hdu 1151 Air Raid DAG最小边覆盖 最大二分匹配的相关文章

hdu 1151 Air Raid (最小路径覆盖)

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3085    Accepted Submission(s): 2004 Problem Description Consider a town where all the streets are one-way and each street leads from on

HDU 1151 Air Raid(最小路径覆盖 = 顶点数 - 最大匹配数)

Air Raid Problem Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same

hdu 1151 Air Raid(二分图最小路径覆盖)

http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4029    Accepted Submission(s): 2675 Problem Description Consider a town where all the st

HDU 1151 Air Raid(最小路径覆盖)

二分图匹配(匈牙利算法的DFS实现) 初始化:g[][]两边顶点的划分情况 建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配 g没有边相连则初始化为0 uN是匹配左边的顶点数,vN是匹配右边的顶点数 调用:res=hungary();输出最大匹配数 优点:适用于稠密图,DFS找增广路,实现简洁易于理解 时间复杂度:O(VE) ***************************************************************************/

hdu 1151 Air Raid 最小路径覆盖

Air Raid Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1151 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting

hdu 1151 Air Raid(最小路径覆盖)

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3894    Accepted Submission(s): 2573 Problem Description Consider a town where all the streets are one-way and each street leads from on

HDU1151:Air Raid(最小边覆盖)

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6528    Accepted Submission(s): 4330 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151 Description: Consider a town where all the stree

hdu 1150 Machine Schedule hdu 1151 Air Raid 匈牙利模版

//两道大水……哦不 两道结论题 结论:二部图的最小覆盖数=二部图的最大匹配数 有向图的最小覆盖数=节点数-二部图的最大匹配数 1 //hdu 1150 2 #include<cstdio> 3 #include<iostream> 4 #include<cmath> 5 #include<algorithm> 6 #include<cstring> 7 #include<cstdlib> 8 #include<queue>

hdu 1151 Air Raid - 二分匹配

Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's