Treasure Exploration
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 6371 | Accepted: 2571 |
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?
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
题意:
给出N(1 ~ 500),M (0 ~ 5000)代表有 N 个点和 M 条单向边。后给出这 M 条单向边 A 到 B。现要覆盖所有点,每次可以选择任何一个点,可以沿着单向边走到终点,走过的点可以重复。输出最少的路径数使覆盖所有的点。
思路:
二分图最大匹配 + Floyd 闭包。求最少路径覆盖(结点数 - 最大匹配)。但是有一点不同就是走过的点可以重复,若直接找最大匹配的话,则走过的点是不可以重复的。
最少路径覆盖就是用最少的路径数去覆盖所有的点,每增加一条匹配边就减少一个路径数,根据题目给出的边直接最大匹配得出来的边数显然会小于等于实际可以匹配的边数。如样例:
5 4
1 2
2 4
3 2
2 5 得出来的应该是 2 条路径,但是因为最大匹配出来的只能是2,那么得出来的最少路径覆盖则是 5 - 2 = 3。说明实际匹配的数是 3 ,但是直接最大匹配出来的却是 2。
解决这个问题自然要增加匹配边,所以要 Floyd 闭包一次,将每个点所能到达的其他点都连起来,再进行一次二分图最大匹配既可以解决问题了。
AC:
#include <cstdio> #include <string.h> using namespace std; int n; int w[505][505],linker[505],vis[505]; bool dfs(int u) { for(int v = 1;v <= n;v++) if(w[u][v] && !vis[v]) { vis[v] = 1; if(linker[v] == -1 || dfs(linker[v])) { linker[v] = u; return true; } } return false; } int hungary() { int res = 0; memset(linker,-1,sizeof(linker)); for(int u = 1;u <= n;u++) { memset(vis,0,sizeof(vis)); if(dfs(u)) res++; } return res; } void floyd() { for(int k = 1;k <= n;k++) for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) if(w[i][k] && w[k][j]) w[i][j] = 1; } int main() { int m; while(~scanf("%d%d",&n,&m) && (n + m)) { memset(w,0,sizeof(w)); while(m--) { int f,t; scanf("%d%d",&f,&t); w[f][t] = 1; } floyd(); printf("%d\n",n - hungary()); } return 0; }