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 streets form no cycles.

With these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper.


Input

Your program should read sets of data. The first line of the input file contains the number of the data sets. Each data set specifies the structure of a town and has the format:

no_of_intersections 
no_of_streets 
S1 E1 
S2 E2 
...... 
Sno_of_streets Eno_of_streets

The first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town‘s streets. The line corresponding to street k (k <= no_of_streets) consists of two positive integers, separated by one blank: Sk (1 <= Sk <= no_of_intersections) - the number of the intersection that is the start of the street, and Ek (1 <= Ek <= no_of_intersections) - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections.

There are no blank lines between consecutive sets of data. Input data are correct. 
Output

The result of the program is on standard output. For each input data set the program prints on a single line, starting from the beginning of the line, one integer: the minimum number of paratroopers required to visit all the intersections in the town. 
Sample Input

2
4
3
3 4
1 3
2 3
3
3
1 3
1 2
2 3

Sample Output

2
1


  最小路径覆盖裸模型。答案 = 点数 - 最大匹配数。

Code

  1 /**
  2  * hdu
  3  * Problem#1151
  4  * Accepted
  5  * Time:0ms
  6  * Memory:1680k
  7  */
  8 #include<iostream>
  9 #include<cstdio>
 10 #include<ctime>
 11 #include<cctype>
 12 #include<cstring>
 13 #include<cstdlib>
 14 #include<fstream>
 15 #include<sstream>
 16 #include<algorithm>
 17 #include<map>
 18 #include<set>
 19 #include<stack>
 20 #include<queue>
 21 #include<vector>
 22 #include<stack>
 23 #ifndef WIN32
 24 #define Auto "%lld"
 25 #else
 26 #define Auto "%I64d"
 27 #endif
 28 using namespace std;
 29 typedef bool boolean;
 30 const signed int inf = (signed)((1u << 31) - 1);
 31 const double eps = 1e-10;
 32 #define smin(a, b) a = min(a, b)
 33 #define smax(a, b) a = max(a, b)
 34 #define max3(a, b, c) max(a, max(b, c))
 35 #define min3(a, b, c) min(a, min(b, c))
 36 template<typename T>
 37 inline boolean readInteger(T& u){
 38     char x;
 39     int aFlag = 1;
 40     while(!isdigit((x = getchar())) && x != ‘-‘ && x != -1);
 41     if(x == -1) {
 42         ungetc(x, stdin);
 43         return false;
 44     }
 45     if(x == ‘-‘){
 46         x = getchar();
 47         aFlag = -1;
 48     }
 49     for(u = x - ‘0‘; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - ‘0‘);
 50     ungetc(x, stdin);
 51     u *= aFlag;
 52     return true;
 53 }
 54
 55 ///map template starts
 56 typedef class Edge{
 57     public:
 58         int end;
 59         int next;
 60         Edge(const int end = 0, const int next = 0):end(end), next(next){}
 61 }Edge;
 62
 63 typedef class MapManager{
 64     public:
 65         int ce;
 66         int *h;
 67         Edge *edge;
 68         MapManager(){}
 69         MapManager(int points, int limit):ce(0) {
 70             h = new int[(const int)(points + 1)];
 71             edge = new Edge[(const int)(limit + 1)];
 72             memset(h, 0, sizeof(int) * (points + 1));
 73         }
 74         inline void addEdge(int from, int end){
 75             edge[++ce] = Edge(end, h[from]);
 76             h[from] = ce;
 77         }
 78         inline void addDoubleEdge(int from, int end){
 79             addEdge(from, end);
 80             addEdge(end, from);
 81         }
 82         Edge& operator [] (int pos) {
 83             return edge[pos];
 84         }
 85         inline void clear() {
 86             delete[] h;
 87             delete[] edge;
 88         }
 89 }MapManager;
 90 #define m_begin(g, i) (g).h[(i)]
 91 ///map template ends
 92
 93 int T;
 94 int n, m;
 95 MapManager g;
 96
 97 inline void init() {
 98     readInteger(n);
 99     readInteger(m);
100     g = MapManager(n, m);
101     for(int i = 0, a, b; i < m; i++) {
102         readInteger(a);
103         readInteger(b);
104         g.addEdge(a, b);
105     }
106 }
107
108 int* match;
109 boolean* vis;
110 boolean dfs(int node) {
111     for(int i = m_begin(g, node); i; i = g[i].next) {
112         int &e = g[i].end;
113         if(vis[e])    continue;
114         vis[e] = true;
115         if(match[e] == -1 || dfs(match[e])) {
116             match[e] = node;
117             return true;
118         }
119     }
120     return false;
121 }
122
123 int res;
124 inline void solve() {
125     res = n;
126     vis = new boolean[2 * n + 1];
127     match = new int[2 * n + 1];
128     memset(match, -1, sizeof(int) * (2 * n + 1));
129     for(int i = 1; i <= n; i++) {
130         memset(vis, false, sizeof(boolean) * (2 * n + 1));
131         if(dfs(i))    res--;
132     }
133     printf("%d\n", res);
134     delete[] vis;
135     delete[] match;
136 }
137
138 int main() {
139     readInteger(T);
140     while(T--) {
141         init();
142         solve();
143     }
144     return 0;
145 }
时间: 2024-10-05 09:31:54

hdu 1151 Air Raid - 二分匹配的相关文章

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

poj 1422 Air Raid (二分匹配)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6520   Accepted: 3877 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 i

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(二分图最小路径覆盖)

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 DAG最小边覆盖 最大二分匹配

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151 题目大意: 城镇之间互相有边,但都是单向的,并且不会构成环,现在派伞兵降落去遍历城镇,问最少最少派多少人去 思路: 转化题意,求用最少的有向边覆盖点        -------->      最小边覆盖数目=顶点数-最大二分匹配数目 注意:这道题目中说得是有向无环的DAG,所以顶点数目不能按照2倍来计算 在DAG中我们假设点集为 -----> Ni     然后建立每个点对应的虚点 ---

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(最小路径覆盖)

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

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

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>