POJ1422 Air Raid

Air Raid

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8625   Accepted: 5155

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

Dhaka 2002

【题解】

最小路径覆盖裸题

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5
 6 inline void read(int &x)
 7 {
 8     x = 0;char ch = getchar(), c = ch;
 9     while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar();
10     while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar();
11     if(c == ‘-‘)x = -x;
12 }
13
14 const int INF = 0x3f3f3f3f;
15
16 struct Edge
17 {
18     int u,v,next;
19     Edge(int _u, int _v, int _next){u = _u, v = _v, next = _next;}
20     Edge(){}
21 }edge[100000];
22
23 int head[10000], cnt;
24
25 inline void insert(int a, int b)
26 {
27     edge[++cnt] = Edge(a,b,head[a]);
28     head[a] = cnt;
29 }
30
31 int n,m,lk[10000],b[10000];
32
33 int dfs(int u)
34 {
35     for(register int pos = head[u];pos;pos = edge[pos].next)
36     {
37         int v = edge[pos].v;
38         if(b[v])continue;
39         b[v] = 1;
40         if(lk[v] == -1 || dfs(lk[v]))
41         {
42             lk[v] = u;
43             return 1;
44         }
45     }
46     return 0;
47 }
48
49 int xiongyali()
50 {
51     int ans = 0;
52     memset(lk, -1, sizeof(lk));
53     for(register int i = 1;i <= n;++ i)
54     {
55         memset(b, 0, sizeof(b));
56         ans += dfs(i);
57     }
58     return ans;
59 }
60
61 int t;
62
63 int main()
64 {
65     read(t);
66     for(;t;--t)
67     {
68         memset(edge, 0, sizeof(edge));
69         memset(head, 0, sizeof(head));
70         cnt = 0;
71         read(n), read(m);
72         register int tmp1,tmp2,i;
73         for(i = 1;i <= m;++ i)
74         {
75             read(tmp1), read(tmp2);
76             insert(tmp1, tmp2);
77         }
78         printf("%d\n", n - xiongyali());
79     }
80     return 0;
81 } 

POJ1422

时间: 2025-01-17 07:15:37

POJ1422 Air Raid的相关文章

POJ1422 Air Raid 【DAG最小路径覆盖】

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

POJ1422 Air Raid【二分图最小路径覆盖】

题目链接: http://poj.org/problem?id=1422 题目大意: 有N个地点和M条有向街道,现在要在点上放一些伞兵,伞兵可以沿着有向街道走,直到不能走为止. 每条边只能被一个伞兵走一次.问:至少放多少伞兵,能使伞兵可以走到图上所有的点. 思路: 很明显的最小路径覆盖问题.先转换为二分图,先将N个点每个点拆成两个点,左边是1~N个点,右 边也是1~N个点.将有向街道变为左边点指向右边点的边. 因为二分图最小路径覆盖 = 点数 - 二分图最大匹配数,则求出结果就是放的最少伞兵数.

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

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

POJ 1422:Air Raid(最大独立集)

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

Air Raid[HDU1151]

Air RaidTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4974 Accepted Submission(s): 3347 Problem DescriptionConsider a town where all the streets are one-way and each street leads from one interse

Air Raid(最小路径覆盖)

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

HDUJ 1151 Air Raid 最大匹配

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