又开贴!

emailed 2008-04-23 06:37:34
A bicycle race is being organized in a land far, far away. There are N town in the land, numbered 1 through N. There are also M one-way roads between the towns. The race will start in town 1 and end in town 2.

How many different ways can the route be set? Two routes are considered different if they do not use the exact same roads.

Input
The first line of input contains two integers N and M (1 ≤ N ≤ 10000, 1 ≤ M ≤ 100000), the number of towns and roads.

Each of the next M lines contains two different integers A and B, representing a road between towns A and B. Towns may be connected by more than one road.

Output
Output the number of distinct routes that can be set on a single line. If that number has more than nine digits, output only the last nine digits of the number. If there are infinitely many routes, output "inf".

Sample Input #1

6 7
1 3
1 4
3 2
4 2
5 6
6 5
3 4

Sample Output #1

3

Sample Input #2

6 8
1 3
1 4
3 2
4 2
5 6
6 5
3 4
4 3

Sample Output #2

inf

Sample Input #3

31 60
1 3
1 3
3 4
3 4
4 5
4 5
5 6
5 6
6 7
6 7
...
...
...
28 29
28 29
29 30
29 30
30 31
30 31
31 2
31 2

Sample Output #3

073741824

...全文
232 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
呵呵,好像没有现成的代码,俺也一贯是纸上谈兵。
emailed 2008-04-24
  • 打赏
  • 举报
回复
还有你看Sample 3,数字太大的话只能输出后9位
emailed 2008-04-24
  • 打赏
  • 举报
回复
嗯,没错阿,但是这个代码好难写啊 。。。。
  • 打赏
  • 举报
回复
这个题就是从节点town1开始对有向图进行遍历,找到所有town1到town2之间的路径。
中间需要注意对回路(路径中某节点指向了它的祖先节点)的特殊处理:如果存在回路的这条路径不能到达town2,那它对结果没有影响;如果存在回路的该路径也能到达town2,那么答案就是无限了。
dobear_0922 2008-04-24
  • 打赏
  • 举报
回复
,,,
emailed 2008-04-24
  • 打赏
  • 举报
回复
to dlyme:
我代码控制能力不行啊 ,给点代码吧
  • 打赏
  • 举报
回复
嗯,先裁掉一些没用的顶点。通过深度优先搜索对顶点进行拓扑排序,然后根据排序后的顺序依次计算从1过来的路径数量,性能是要好得多。
emailed 2008-04-24
  • 打赏
  • 举报
回复
还是不行啊。。。给个代码吧
tailzhou 2008-04-24
  • 打赏
  • 举报
回复
http://forum.byr.edu.cn/wForum/boardcon.php?bid=212&id=5777&ftype=3
emailed 2008-04-24
  • 打赏
  • 举报
回复
可以把那个解题报告的网址发一下吗
tailzhou 2008-04-24
  • 打赏
  • 举报
回复
这么大的图做直接做遍历是不现实的。
==》
这么大的图直接做遍历求解是不现实的。

因为求路径数目,图中的节点需要多次遍历到,这样复杂度就太高了;
tailzhou 2008-04-24
  • 打赏
  • 举报
回复
M (1 ≤ N ≤ 10000, 1 ≤ M ≤ 100000),
这么大的图做直接做遍历是不现实的。

找到一个解题报告:

发信人: moyuji (moyuji), 信区: ACM_ICPC
标 题: [解题报告]BICIKLI
发信站: 北邮人论坛 (Wed Mar 28 09:22:33 2007), 站内

We model the network of cities and roads with a directed graph.
Note that vertices unreachable from the start node and vertices from which the end node is not reachable are of no use in constructing the bicycle route – we can remove them from the graph.
An infinite number of routes exist only if there is a cycle in the new graph. If there is a cycle, we can go from the start node to the cycle, go about the cycle any number of times and then continue on to the end node.
If there are no cycles, then the number of routes is finite and the graph is said to be acyclic (directed acyclic graph, DAG). Such a graph has what is called a topological ordering: if there is a path from vertex A to vertex B, then A comes before B in the topological ordering. We can find one topological ordering of the graph using a depth-first-search.
Once we've found a topological ordering, we consider vertices in that order and for each vertex V we calculate the number of routes from vertex 1 to that vertex: examine the edges going into V and add up the numbers of ways to reach the preceding vertices (those numbers have already been calculated by now).

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧