Treasure Exploration
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 8879 | Accepted: 3635 |
Description
Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings to you.
Recently, a company named EUC (Exploring the Unknown Company) plan
to explore an unknown place on Mars, which is considered full of
treasure. For fast development of technology and bad environment for
human beings, EUC sends some robots to explore the treasure.
To make it easy, we use a graph, which is formed by N points (these N
points are numbered from 1 to N), to represent the places to be
explored. And some points are connected by one-way road, which means
that, through the road, a robot can only move from one end to the other
end, but cannot move back. For some unknown reasons, there is no circle
in this graph. The robots can be sent to any point from Earth by
rockets. After landing, the robot can visit some points through the
roads, and it can choose some points, which are on its roads, to
explore. You should notice that the roads of two different robots may
contain some same point.
For financial reason, EUC wants to use minimal number of robots to explore all the points on Mars.
As an ICPCer, who has excellent programming skill, can your help EUC?
Input
The
input will consist of several test cases. For each test case, two
integers N (1 <= N <= 500) and M (0 <= M <= 5000) are given
in the first line, indicating the number of points and the number of
one-way roads in the graph respectively. Each of the following M lines
contains two different integers A and B, indicating there is a one-way
from A to B (0 < A, B <= N). The input is terminated by a single
line with two zeros.
Output
For each test of the input, print a line containing the least robots needed.
Sample Input
1 0 2 1 1 2 2 0 0 0
Sample Output
1 1 2
Source
POJ Monthly--2005.08.28,Li Haoyuan
【题解】
二分图可重最小路径覆盖裸题
先求传递闭包,然后连边
注意时间,数组尽量开小
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[250010]; 22 23 int head[510], 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[510],b[510],g[510][610]; 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 main() 62 { 63 while(scanf("%d %d", &n, &m) != EOF && (n + m)) 64 { 65 memset(edge, 0, sizeof(edge)); 66 memset(head, 0, sizeof(head)); 67 memset(g, 0, sizeof(g)); 68 cnt = 0; 69 int tmp1, tmp2; 70 for(register int i = 1;i <= m;++ i) 71 { 72 read(tmp1), read(tmp2); 73 g[tmp1][tmp2] = 1; 74 } 75 for(register int k = 1;k <= n;++ k) 76 for(int i = 1;i <= n;++ i) 77 for(int j = 1;j <= n;++j) 78 g[i][j] |= g[i][k] && g[k][j]; 79 for(register int i = 1;i <= n;++ i) 80 for(int j = 1;j <= n;++ j) 81 if(g[i][j]) insert(i, j); 82 printf("%d\n", n - xiongyali()); 83 } 84 return 0; 85 }
POJ2594