usaco4.3.2 Street Race

一 原题

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



二 分析

给定一个有向图,有一个起点和一个终点.保证:(1)起点可以到达图中每个点 (2)每个点都可以到达终点 (3)终点的出度为0. 有两小问:
(a) 有哪些点是从起点出发到达终点前必须经过的
(b) 可以删去哪些点(一次只删一个). 删除点P后形成两个图,这两个图的点不可达, 每个图都满足(1)(2)(3)三条规则, 且第一个图以P为终点, 第二个图以P为起点
第一小问直接试着删去一个点,看删去后是否还有从起点到终点的路径即可. 第二小问我看了nocow上的一个解释:" 首先可以确定第二问的解集是第一问的子集,所以我们可以第一问得出的每个点深搜,记录下可以到达的点。然后去掉该点,从起点深搜。如果不存在两次深搜皆可到达的点,就说明它是分割". 正确性我并不明白.


三 代码

运行结果:
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.


AC代码:
/*
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();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值