hdu 1151 Air Raid - 二分匹配
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
/**
* hdu
* Problem#1151
* Accepted
* Time:0ms
* Memory:1680k
*/
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const double eps = 1e-;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} ///map template starts
typedef class Edge{
public:
int end;
int next;
Edge(const int end = , const int next = ):end(end), next(next){}
}Edge; typedef class MapManager{
public:
int ce;
int *h;
Edge *edge;
MapManager(){}
MapManager(int points, int limit):ce() {
h = new int[(const int)(points + )];
edge = new Edge[(const int)(limit + )];
memset(h, , sizeof(int) * (points + ));
}
inline void addEdge(int from, int end){
edge[++ce] = Edge(end, h[from]);
h[from] = ce;
}
inline void addDoubleEdge(int from, int end){
addEdge(from, end);
addEdge(end, from);
}
Edge& operator [] (int pos) {
return edge[pos];
}
inline void clear() {
delete[] h;
delete[] edge;
}
}MapManager;
#define m_begin(g, i) (g).h[(i)]
///map template ends int T;
int n, m;
MapManager g; inline void init() {
readInteger(n);
readInteger(m);
g = MapManager(n, m);
for(int i = , a, b; i < m; i++) {
readInteger(a);
readInteger(b);
g.addEdge(a, b);
}
} int* match;
boolean* vis;
boolean dfs(int node) {
for(int i = m_begin(g, node); i; i = g[i].next) {
int &e = g[i].end;
if(vis[e]) continue;
vis[e] = true;
if(match[e] == - || dfs(match[e])) {
match[e] = node;
return true;
}
}
return false;
} int res;
inline void solve() {
res = n;
vis = new boolean[ * n + ];
match = new int[ * n + ];
memset(match, -, sizeof(int) * ( * n + ));
for(int i = ; i <= n; i++) {
memset(vis, false, sizeof(boolean) * ( * n + ));
if(dfs(i)) res--;
}
printf("%d\n", res);
delete[] vis;
delete[] match;
} int main() {
readInteger(T);
while(T--) {
init();
solve();
}
return ;
}
hdu 1151 Air Raid - 二分匹配的更多相关文章
- hdu 1151 Air Raid(二分图最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Time Limit: 1000MS Memory Limit: 10000K To ...
- hdu 1151 Air Raid DAG最小边覆盖 最大二分匹配
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151 题目大意: 城镇之间互相有边,但都是单向的,并且不会构成环,现在派伞兵降落去遍历城镇,问最少最少 ...
- hdu 1150 Machine Schedule hdu 1151 Air Raid 匈牙利模版
//两道大水……哦不 两道结论题 结论:二部图的最小覆盖数=二部图的最大匹配数 有向图的最小覆盖数=节点数-二部图的最大匹配数 //hdu 1150 #include<cstdio> #i ...
- poj 1422 Air Raid (二分匹配)
Air Raid Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6520 Accepted: 3877 Descript ...
- hdu1151 Air Raid 二分匹配
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151 求最小路径覆盖 二分图最小路径覆盖=点的个数-最大匹配. 代码: #include<ios ...
- hdu - 1151 Air Raid(有向无环图的最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=1151 在一个城市里有n个地点和k条道路,道路都是单向的,并且不存在环.(DAG) 现在伞兵需要去n个地点视察,伞 ...
- HDU 1151 Air Raid(最小路径覆盖)
题目大意: 有n个城市,m条道路,城市的道路是单向. 现在我们的伞兵要降落在城市里,然后我门的伞兵要搜索所有道路.问我们最少占领多少个城市就可以搜索所有的道路了. 我们可以沿着道路向前走到达另一个城 ...
- (step6.3.4)hdu 1151(Air Raid——最小路径覆盖)
题意: 一个镇里所有的路都是单向路且不会组成回路. 派一些伞兵去那个镇里,要到达所有的路口,有一些或者没有伞兵可以不去那些路口,只要其他人能完成这个任务.每个在一个路口着陆了的伞兵可以沿着街去 ...
- HDU 1151 - Air Raid
很明显求最小路径覆盖 就是求最大匹配 #include <iostream> #include <cstdio> #include <cstring> #inclu ...
随机推荐
- cocos游戏开发小白教程网站
<Quick-Cocos2d-x v3.3小白书系列教程> <Quick-Cocos2d-x初学者游戏教程>
- 实验验证sys和system用户全库导出的区别
我们在做逻辑数据泵全库导出的时候,有两种流行的写法,一种是sys用户导出,一种是使用system用户导出. 现在想知道二者之间有什么区别?实验验证之前不妨先思考一下: sys和system用户的权限区 ...
- 003-SqlHelper.cs/Web.config
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访 ...
- tortoise svn中更改用户
1. Open Windows Explorer.2. Right-click anywhere in the window.3. Click TortoiseSVN → Settings.4. Cl ...
- Oracle / PLSQL函数 - DECODE
1.DECODE( expression , search , result [, search , result]... [, default] ) 参数说明: expression : 表中的某一 ...
- C#-----创建DataTable对象
//DataTable表示内存中数据的一个表 DataTable dt = new DataTable(); /** * public DataColumn Add(string columnName ...
- OpenCV-3.3.0测试
安装包目录下/samples/cpp里是各种例程 其中example_cmake里CMakeLists.txt已写好,直接cmake,make就可以,example.cpp是一个调用笔记本摄像头并显示 ...
- JAVA基础3---JVM内存模型
Java虚拟机执行Java程序的时候需要使用一定的内存,根据不同的使用场景划分不同的内存区域.有公用的区域随着Java程序的启动而创建:有线程私有的区域依赖线程的启动而创建 JVM内存模型大致可以分为 ...
- 排名函数——ROW_NUMBER()、RANK()、DENSE_RANK()和NTILE(n)
ROW_NUMBER()函数:行号,根据作为参数传递给这个函数的ORDER BY子句的值,返回一个不断递增的整数值.如果ROW_NUMBER的ORDER BY的值和结果集中的顺序相匹配,返回值将是递增 ...
- px-pt-dp-rem像素单位的换算问题
px-pt-dp-rem像素单位的换算问题 dp 的意思从 MDPI 到 XXXHDPI 每单位物理尺寸的像素数越来越大.也就是说 mdpi 时 1dp = 1pxxxxhdpi 时 1dp = 4p ...