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.
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.
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
【题解】
最小路径覆盖裸题
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 }