[USACO4.3.2]Street Race

本文针对StreetRace竞赛进行了深入解析,提供了高效求解方法。通过DFS算法确定比赛中必经的检查点,并找出比赛路线的分割点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Street Race

 

IOI'95

Figure 1 gives an example of a course for a street race. You see some points, labeled from 0 to N (here, N=9), and some arrows connecting them. Point 0 is the start of the race; point N is the finish. The arrows represent one-way streets. The participants of the race move from point to point via the streets, in the direction of the arrows only. At each point, a participant may choose any outgoing arrow.

 
Figure 1: A street course with 10 points

A well-formed course has the following properties:

  • Every point in the course can be reached from the start.
  • The finish can be reached from each point in the course.
  • The finish has no outgoing arrows.

A participant does not have to visit every point of the course to reach the finish. Some points, however, are unavoidable. In the example, these are points 0, 3, 6, and 9. Given a well-formed course, your program must determine the set of unavoidable points that all participants have to visit, excluding start and finish.

Suppose the race has to be held on two consecutive days. For that purpose the course has to be split into two courses, one for each day. On the first day, the start is at point 0 and the finish at some `splitting point'. On the second day, the start is at this splitting point and the finish is at point N. Given a well-formed course, your program must also determine the set of splitting points. A point S is a splitting point for the well-formed course C if S differs from the star t and the finish of C, and the course can be split into two well-formed courses that (1) have no common arrows and (2) have S as their only common point, with S appearing as the finish of one and the start of the other. In the example, only point 3 is a splitting point.

PROGRAM NAME: race3

INPUT FORMAT

The input file contains a well-formed course with at most 50 points and at most 100 arrows. There are N+2 lines in the file. The first N+1 lines contain the endpoints of the arrows that leave from the points 0 through N respectively. Each of these lines ends with the number -2. The last line contains only the number -1.

SAMPLE INPUT (file race3.in)

1 2 -2
3 -2
3 -2
5 4 -2
6 4 -2
6 -2
7 8 -2
9 -2
5 9 -2
-2
-1

OUTPUT FORMAT

Your program should write two lines. The first line should contain the number of unavoidable points in the input course, followed by the labels of these points, in ascending order. The second line should contain the number of splitting points of the input course, followed by the labels of all these points, in ascending order.

SAMPLE OUTPUT (file race3.out)

2 3 6
1 3

 
题意:http://www.nocow.cn/index.php/Translate:USACO/race3

 
历程:
对于第一问,显然可以将某个点去点,然后大力DFS看能不能到达终点,这个是不会超时的。
第二个其实也可以,但是当时我觉得,也许可以用一些高端技巧做。

 
于是开始用Tarjan,用low,判桥……瞎搞一通发现不能用。于是果断删去一大段代码,重新写了DFS
具体是这样的:
同时预处理出第二个数组(封包传递,floyd即可,编码快!)
然后枚举每一个,假设断掉这个点,看这个点能否访问到之前的点。
好像远远不会超。

 
/* ID:cqz15311 LANG:C++ PROG:race3 */ #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; const int maxn = 55; int n; int Ans[maxn],ans[maxn]; bool vis[maxn]; int Map[maxn][maxn],MM[maxn][maxn]; bool dfs(int u){ if (u == (n-1)) return true; vis[u] = true; for (int v=0;v<n;v++){ if (Map[u][v] && !vis[v]){ if (dfs(v)) return true; } } return false; } int main(){ freopen("race3.in","r",stdin); freopen("race3.out","w",stdout); n = 0; int c; scanf("%d",&c); memset(Map,false,sizeof(Map)); while (c != -1){ while (c != -2){ Map[n][c] = true; scanf("%d",&c); } n++; scanf("%d",&c); } /*对于第一个询问,只需要暴力将某个点去掉,然后看 能否从起点到终点(DFS一下即可)*/ ans[0] = 0; for (int i=1;i<n-1;i++){ memset(vis,false,sizeof(vis)); vis[i] = true; if (!dfs(0)){ ans[++ans[0]] = i; } } printf("%d",ans[0]); for (int i=1;i<=ans[0];i++){ printf(" %d",ans[i]); } puts(""); //第二个 for (int i=0;i<n;i++){ for (int j=0;j<n;j++){ MM[i][j] = Map[i][j]; } } for (int k=0;k<n;k++) for (int i=0;i<n;i++) for (int j=0;j<n;j++) if (MM[i][k] && MM[k][j]) MM[i][j] = true; for (int k=1;k<n-1;k++){ memset(vis,false,sizeof(vis)); vis[k] = true; if (!dfs(0)){ bool flag = true; for (int i=0;i<n;i++){ // printf("(%d,%d):%d\n",k,i,Map[k][i]); if ((k!=i) && (vis[i]) && (MM[k][i])){ flag = false; break; } } if (flag) Ans[++Ans[0]] = k; } } printf("%d",Ans[0]); for (int i=1;i<=Ans[0];i++) printf(" %d",Ans[i]); puts("");//USACO特殊最后回车…… fclose(stdin); fclose(stdout); return 0; } /* Executing... Test 1: TEST OK [0.000 secs, 4204 KB] Test 2: TEST OK [0.000 secs, 4204 KB] Test 3: TEST OK [0.000 secs, 4204 KB] Test 4: TEST OK [0.000 secs, 4204 KB] Test 5: TEST OK [0.000 secs, 4204 KB] Test 6: TEST OK [0.000 secs, 4204 KB] Test 7: TEST OK [0.000 secs, 4204 KB] Test 8: TEST OK [0.000 secs, 4204 KB] Test 9: TEST OK [0.000 secs, 4204 KB] Test 10: TEST OK [0.000 secs, 4204 KB] Test 11: TEST OK [0.000 secs, 4204 KB] All tests OK. Your program ('race3') produced all correct answers! This is your submission #3 for this problem. Congratulations! */

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值