您的位置:首页 > 其它

[USACO4.3.2]Street Race

2017-08-07 21:22 288 查看
[b]新博客参见emoairx.github.io[/b]Street RaceIOI'95Figure 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 pointsA 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 ifS 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 thepoints 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 lineshould 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:cqz15311LANG: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[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 yoursubmission #3 for this problem.  Congratulations!*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: