传送门:http://poj.org/problem?id=1422

Air Raid

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9303   Accepted: 5570

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 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

Source

题意概括:

N个路口 M 条单向路,安排若干个士兵,这些士兵只会沿着当前所在结点路径走,问最少安排多少个士兵就可以把所有路口巡逻完毕。

解题思路:

拆点:

把原图的每个点V拆成VxVx和VyVy两个点,如果有一条有向边A->B,那么就加边Ax−>By。这样就得到了一个二分图。

最小路径覆盖=原图的结点数-新图的最大匹配数。

一开始每个点都是独立的为一条路径,总共有n条不相交路径。我们每次在二分图里找一条匹配边就相当于把两条路径合成了一条路径,也就相当于路径数减少了1。所以找到了几条匹配边,路径数就减少了多少。所以有最小路径覆盖=原图的结点数-新图的最大匹配数。

AC code(静态邻接表):

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = ;
struct Edge
{
int v, nxt;
}edge[MAXN]; int head[MAXN*MAXN], cnt;
int linker[MAXN];
bool used[MAXN];
int N, M; void init()
{
memset(head, -, sizeof(head));
memset(linker, -, sizeof(linker));
cnt = ;
} void add(int from, int to)
{
edge[cnt].v = to;
edge[cnt].nxt = head[from];
head[from] = cnt++;
} bool Find(int x)
{
int v;
for(int i = head[x]; i != -; i = edge[i].nxt){
v = edge[i].v;
if(used[v]) continue;
used[v] = true;
if(linker[v] == - || Find(linker[v])){
linker[v] = x;
return true;
}
}
return false;
} int main()
{
int T_case, u, v, ans;
scanf("%d", &T_case);
while(T_case--){
init();
scanf("%d %d", &N, &M);
while(M--){
scanf("%d%d", &u, &v);
add(u, v);
}
ans = ;
for(int i = ; i <= N; i++){
memset(used, , sizeof(used));
if(Find(i)) ans++;
}
printf("%d\n", N-ans);
}
return ;
}

POJ Air Raid 【DAG的最小不相交路径覆盖】的更多相关文章

  1. Air Raid POJ - 1422 【有向无环图(DAG)的最小路径覆盖【最小不相交路径覆盖】 模板题】

    Consider a town where all the streets are one-way and each street leads from one intersection to ano ...

  2. P2172 [国家集训队]部落战争 二分图最小不相交路径覆盖

    二分图最小不相交路径覆盖 #include<bits/stdc++.h> using namespace std; ; ; ; ], nxt[MAXM << ], f[MAXM ...

  3. [luoguP2765] 魔术球问题(最大流—最小不相交路径覆盖)

    传送门 枚举球的个数 num 如果 i < j && (i + j) 是完全平方数,那么 i -> j' 连一条边 再加一个超级源点 s,s -> i 再加一个超级汇 ...

  4. POJ1422Air Raid(二分图,最小不相交路径覆盖)

    Air Raid Consider a town where all the streets are one-way and each street leads from one intersecti ...

  5. POJ 2594 Treasure Exploration 最小可相交路径覆盖

    最小路径覆盖 DAG的最小可相交路径覆盖: 算法:先用floyd求出原图的传递闭包,即如果a到b有路径,那么就加边a->b.然后就转化成了最小不相交路径覆盖问题. 这里解释一下floyd的作用如 ...

  6. HDU 4862 Jump(最小K路径覆盖)

    输入一个n×m网格图,每个结点的值为0-9,可以从任意点出发不超过k次,走完每个点且仅访问每个结点一次,问最终的能量最大值.不可全部走完的情况输出-1. 初始能量为0. 而结点(x,y)可以跳跃到结点 ...

  7. 网络费用流-最小k路径覆盖

    多校联赛第一场(hdu4862) Jump Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. POJ 3308 Paratroopers (对数转换+最小点权覆盖)

    题意 敌人侵略r*c的地图.为了消灭敌人,可以在某一行或者某一列安置超级大炮.每一个大炮可以瞬间消灭这一行(或者列)的敌人.安装消灭第i行的大炮消费是ri.安装消灭第j行的大炮消费是ci现在有n个敌人 ...

  9. POJ - 2125 Destroying The Graph (最小点权覆盖)

    题意:给一张图,现在要删去所有的边,删去一个点的所有入边和所有出边都有其对应\(W_{i+}\)和\(W_{i-}\).求删去该图的最小花费,并输出解 分析:简而言之就是用最小权值的点集去覆盖所有的边 ...

随机推荐

  1. python查看模块版本及所在文件夹

    # 以Numpy为例 第一种方法:import numpy as np np.__version__ >>> '1.12.1' np.__file__ >>> '/ ...

  2. 解决matplotlib绘图中文乱码

    # 指定默认字体 下面三条代码用来解决绘图中出现的乱码 matplotlib.rcParams['font.sans-serif'] = ['SimHei'] matplotlib.rcParams[ ...

  3. 最好用的数据存储Easy Save2讲解

    转载:http://www.manew.com/thread-100109-1-1.html   今天抽时间学习了“Easy Save2”插件,版本v2.6.3  我个人觉得这个插件是做数据存取最好的 ...

  4. Nginx+Keepalived配置

    1. Nginx安装 (1) 环境:分别在2台服务器上部署nginx且步骤一致: 如192.138.86.1和192.138.86.2 (2) 下载官网最新稳定版,地址:https://nginx.o ...

  5. 很有用的PHP笔试题系列三

    1. 什么事面向对象?主要特征是什么? 面向对象是程序的一种设计方式,它利于提高程序的重用性,使程序结构更加清晰.主要特征:封装.继承.多态. 2. SESSION 与 COOKIE的区别是什么,请从 ...

  6. [转]NLog Layout Renderers ${}

    https://github.com/nlog/NLog/wiki/Layout-Renderers Layout renderers are template macros that are use ...

  7. log4net日记文件路径动态配置

    在项目开发过程中,部署的服务器越来越多,查看日记的时候需要每台服务器去找日记看,这对运维人员来说是一个很不友好的方式.在此基础上就提出将所有日记统一到一台服务器上进行存放,并按照产生日记的服务器分文件 ...

  8. sqlServer游标的使用

    USE [PatPD1]GO/****** Object:  UserDefinedFunction [dbo].[fun_GetConditionInner]    Script Date: 201 ...

  9. mac的svn

    http://xclient.info/s/cornerstone.html?t=c5242a66e53f1d866afe8c42aace2738c04ce9ee#versions 破解版的地址 打开 ...

  10. SpagoBI 和 开源ERP(iDempiere)整合入门

    Created by 蓝色布鲁斯,QQ32876341,blog http://www.cnblogs.com/zzyan/ iDempiere官方中文wiki主页 http://wiki.idemp ...