C++——【USACO 4.3.2】——Street Race

来源:互联网 发布:wpf编程宝典 pdf 编辑:程序博客网 时间:2024/05/22 05:18

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 -23 -23 -25 4 -26 4 -26 -27 8 -29 -25 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 61 3

街头比赛
IOI 95
图1给出了一个街道比赛的例子。你可以看到一些点,从0到N(这里,N = 9),还有一些箭头连接它们。点0是比赛的起点;点N是终点。箭头表示单行道。比赛的参与者从一个点到另一个点,在街道上,人只能走在箭头的方向。在每个点上,参与者可以选择任意的输出箭头。
图1:有10个点的街头路线
一个结构良好的图有以下特点:
图的每一点都可以从一开始就达到。
可以从每一个点到达终点。
终点没有延伸出的边。
参与者不必访问图的每一个点,才能到达终点。然而,有些点是不可避免的。在这个例子中,这些是0、3、6和9。给定一个格式良好的课图,您的程序必须确定所有参与者必须访问的不可避免的点集,不包括开始和结束。
假设比赛必须连续两天举行。为了达到这个目的,图必须分成两段。第一天,起点为0点,终点为“分裂点”。在第2天,开始是在这个分裂点,终点是在N点。给定一个格式良好的图,你的程序也必须确定分裂点的集合。S点是一个格式良好的分割点C,C不是终点,和过程可以分成两个格式良好的图(1)和没有共同的箭头的图(2),作为他们唯一的共同点,C是第一天的终点也是第二天的起点。在这个例子中,只有点3是一个分裂点。
项目名称:race3
输入格式
输入文件包含一个格式良好的图,最多50个点,最多100个箭头。文件中有N + 2行。第一个N + 1行包含从0点到N的箭头的端点。每行以- 2号结束。最后一行只包含数字- 1。
示例输入(文件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
输出格式
你的程序应该写两行。第一行应该包含输入过程中不可避免的点的数量,后面跟着这些点,按升序排列。第二行应该包含输入过程的分裂点的数目,后面跟着这些点的,按升序排列。
样例输出(文件race3.out)
2 3 6
1 3


/*ID : mcdonne1LANG : C++TASK : race3*/#pragma GCC optimize("O3")#include <cstdio>#include <cctype>#include <cstring>#include <vector>using namespace std;int n, x, cnt;int to[300], next[300], first[60], went[60];bool flag;vector <int> v;vector <int> m;inline int read () {int i = 0, f = 1;char c = getchar();while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}while (isdigit(c)) i = (i << 3) + (i << 1) + c - 48, c = getchar();return i * f;}inline void add (int x, int y) {to[++cnt] = y;next[cnt] = first[x];first[x] = cnt;}void dfs(int x){went[x] = 1;for (int i = first[x]; i != -1; i = next[i]) if (!went[to[i]]) dfs(to[i]);}void redfs1(int x){went[x] = 1;if (x == cnt) return;for (int i = first[x]; i != -1; i = next[i])if(!went[to[i]]) redfs1(to[i]);}void redfs2(int x){went[x] = 2;for (int i = first[x]; i != -1; i = next[i])if(went[to[i]] == 1) {flag = false;return;} else if(went[to[i]] != 2) redfs2(to[i]);}int main () {freopen ("race3.in", "r", stdin);freopen ("race3.out", "w", stdout);memset (to, -1, sizeof(to));memset (next, -1, sizeof(next));memset (first, -1, sizeof(first));for (;;) {x = read();if (x == -1) {--n; break;}if (x == -2) {++n; continue;}add (n, x);}for (int i = 1; i < n; ++i) {memset (went, 0, sizeof(went));went[i] = 1;dfs(0);if(!went[n]) v.push_back(i);}for (int i = 0; i < v.size(); i++) {memset (went, 0, sizeof(went));cnt = v[i];flag = 1;redfs1(0);redfs2(cnt);if(flag) m.push_back(cnt);}printf("%d", v.size());for (int i = 0; i < v.size(); i++) printf (" %d", v[i]);putchar('\n');printf("%d", m.size());for (int i = 0; i < m.size(); i++) printf (" %d", m[i]);putchar('\n');return 0;}


原创粉丝点击