一 原题
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
二 分析
三 代码
USER: Qi Shen [maxkibb3] TASK: race3 LANG: JAVA Compiling... Compile: OK Executing... Test 1: TEST OK [0.259 secs, -1194644 KB] Test 2: TEST OK [0.187 secs, -1194644 KB] Test 3: TEST OK [0.144 secs, -1194644 KB] Test 4: TEST OK [0.130 secs, -1194644 KB] Test 5: TEST OK [0.130 secs, -1194644 KB] Test 6: TEST OK [0.122 secs, -1194644 KB] Test 7: TEST OK [0.144 secs, -1194644 KB] Test 8: TEST OK [0.137 secs, -1194644 KB] Test 9: TEST OK [0.144 secs, -1194644 KB] Test 10: TEST OK [0.151 secs, -1194644 KB] Test 11: TEST OK [0.137 secs, -1194644 KB] All tests OK.
YOUR PROGRAM ('race3') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.
/*
ID:maxkibb3
LANG:JAVA
PROB:race3
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class race3 {
int n = 0;
int[][] next = new int[55][55];
int[] next_size = new int[55];
boolean[] vis = new boolean[55];
int[] color = new int[55];
ArrayList<Integer> ans1 = new ArrayList<>(), ans2 = new ArrayList<>();
void init() throws IOException {
Scanner sc = new Scanner(new FileReader("race3.in"));
boolean eof = false;
while(true) {
if(eof) break;
while(true) {
int tmp = sc.nextInt();
if(tmp == -2) {
n++;
break;
}
else if(tmp == -1) {
eof = true;
break;
}
else next[n][next_size[n]++] = tmp;
}
}
}
boolean erase_dfs(int idx, int erase_id) {
if(idx == n - 1) return true;
for(int i = 0; i < next_size[idx]; i++) {
if(next[idx][i] == erase_id) continue;
if(vis[next[idx][i]]) continue;
vis[next[idx][i]] = true;
if(erase_dfs(next[idx][i], erase_id)) return true;
}
return false;
}
void dfs(int idx) {
for(int i = 0; i < next_size[idx]; i++) {
if(color[next[idx][i]] != 0) continue;
color[next[idx][i]] = 1;
dfs(next[idx][i]);
}
}
void solve1() {
for(int i = 1; i < n - 1; i++) {
for(int j = 1; j < n; j++) vis[j] = false;
vis[0] = true;
if(erase_dfs(0, i)) continue;
ans1.add(i);
}
}
void solve2() {
for(int i = 0; i < ans1.size(); i++) {
for(int j = 0; j < n; j++) vis[j] = false;
vis[0] = true;
erase_dfs(0, ans1.get(i));
for(int j = 0; j < n; j++) color[j] = 0;
color[ans1.get(i)] = 1;
dfs(ans1.get(i));
boolean isSplit = true;
for(int j = 0; j < n; j++) {
if(vis[j] && color[j] != 0) {
isSplit = false;
break;
}
}
if(isSplit) ans2.add(ans1.get(i));
}
}
void print() throws IOException {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("race3.out")));
out.print(ans1.size());
for(int i = 0; i < ans1.size(); i++) out.print(" " + ans1.get(i));
out.print("\n" + ans2.size());
for(int i = 0; i < ans2.size(); i++) out.print(" " + ans2.get(i));
out.println();
out.close();
}
void run() throws IOException {
init();
solve1();
solve2();
print();
}
public static void main(String[] args) throws IOException {
new race3().run();
}
}