usaco4.3.2 Street Race

USACO4.3.2——质数方阵暴力解法 USACO4.3.2——质数方阵暴力解法 题面链接:这里; 读过题后,就不难发现这是一道填数题,先说填法,大多数人一开始肯定想到一行行填,一列列填,填完后整体判断——大数据直接把你T废: 于是我们就开始关注一些已经确定的点,确定的点越多,可供选择的素数就越少,运行速度就越快,请看下图: 图中的深红色(最左上角)表示题目中确定的点,我们先填第一行第一列(红色),这样有一个约束条件(一头确定),再... 阅读详情

一 原题

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();
    }
}


usaco 4.3.2-prime3 自己写的代码,真是的还字少了不行。。。。。。。。。。。。。。。。。。。。。 立即下载

相关推荐

kepserverEX6创建tcpserver接入自有协议(使用ucon接入)

kepserverEX6创建tcpserver接入自有协议

qq_38388813的博客 1631

质数方阵(USACO 4.3.2)

质数方阵之 《35余次的提交,7天的思考,两个人的帮助,终筑一AC,由此可见,AC是一种幻想》

Young_20220202的博客 1243

数据结构实验1.2—高精度计算PI值(西工大)

这是数据结构第二题的题目,是让我们根据用户的需求来计算派的值 这是题目的详细描述,刚看到这道题的时候,我是真的头大,完全没有思路,于是查找了一些大佬的笔记后才开始编写 这是我在写这些代码是所运用到的数学反三角函数幂次展开式的知识。 下面是正确的代码以及我的一些见解,和需要注意的一些地方。希望能对你有所帮助。如有更加精简的算法,请在评论区留下你的算法!!!谢谢!!! #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdl

abcdef0623的博客 5831

C++——【USACO 4.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;

McDonnell_Douglas的博客 674

智能座舱(七)——智能音频系统

本文剖析车载智能音频系统的核心技术,聚焦音响、功放、麦克风和广播收音机四大组件。音响系统通过多声道布局(如23扬声器阵列)结合动态声场校准算法,实现三维声场覆盖;功放系统采用高效D类技术,支持OTA音效调整和故障自愈;麦克风阵列运用波束成形技术实现精准拾音,并保障隐私安全;广播系统融合数字与网络技术提升音质。核心在于硬件标准化与算法差异化的协同,未来将朝集成化、AI个性化方向发展。

DC20160101的博客 1573

NV 英伟达2022数字芯片笔试题

作者:数字IC打工人 链接:【开卷】第27期 NV 英伟达2022数字芯片笔试题_笔经面经_牛客网 来源:牛客网 1、Single Choice: Given thefollowing FIFO and rules, how deep does the FIFO need to be to preventunderflow or overflow? RULES: clk_A=25MHz; clk_B=100MHz; en_B is periodically asserted: period.

weixin_44072819的博客 6387

【CJOJ1793】【USACO 4.3.2】素数方阵

题面Description在下面的方格中,每行,每列,以及两条对角线上的数字可以看作是五位的素数。方格中的行按照从左到右的顺序组成一个素数,而列按照从上到下的顺序。两条对角线也是按照从左到右的顺序来组成。这些素数各个数位上的和必须相等。 左上角的数字是预先定好的。 一个素数可能在方阵中重复多次。 如果不只有一个解,将它们全部输出(按照这25个数字组成的25位数的大小排序)。 一个五位的素数开

小蒟蒻yyb的博客 4106

【机器学习实战中阶】音乐流派分类-自动化分类不同音乐风格

数据下载地址:https://www.kaggle.com/datasets/andradaolteanu/gtzan-dataset-music-genre-classification/data背景音乐。专家们长期以来一直在尝试理解声音以及是什么使一首歌曲与另一首不同。如何可视化声音。是什么使一个音调与另一个不同。这个数据集希望能提供实现这些目标的机会。内容原始流派 - 包含10种流派,每种流派有100个音频文件,每个文件长度为30秒(著名的GTZAN数据集,声音的MNIST)

2145

[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 r...

qq_37665716的博客 281

GBDT算法详解

本文详细介绍了GBDT算法的原理和特点,并展示了其在回归和分类任务中的应用。首先介绍了GBDT算法的基本思想和公式,然后展示了如何在回归数据集使用GBDT进行回归任务,以及如何在分类数据集上使用GBDT进行文本分类任务。

甜盐的博客 2540

(ICLR 2025)【源码讲解+复现】TIMER-XL: Long-Context Transformers for Unified Time Series Forecasting

本文提出了一种统一的多变量时序预测模型Timer-XL,其核心创新包括:1)采用自回归解码器架构,将多变量时序预测统一为next-token预测问题;2)设计TimeAttention机制,通过相对位置编码和因果掩码捕获时序位置和变量间交互;3)支持长上下文预训练以提升零样本迁移能力。模型架构包含嵌入层、时间感知注意力块和预测头,采用MSE损失进行端到端训练。实验表明该方法在多种时序任务上表现优异,特别是在长跨度预测和少样本场景下。代码实现提供了完整的模型定义、数据加载和训练流程,支持自定义配置参数。

1146

USACO4.3.2 Street Race (race3)

第一问n-2次spfa,枚举每一个不可避免的路口k,spfa时遇到k时continue,如果从起点无法走到终点则k为不可避免的路口。 第二问,显然中间路口一定是一个不可避免路口,所以先枚举每一个不可避免路口k,以k为起点spfa,如果从比k编号小的节点回到了k,则k一定不是中间路口。否则k一定是中间路口。/* ID:shijiey1 PROG:race3 LANG:C++ */ #include

Shion的专栏 483

华为非AI方向0603笔试真题-爆破小游戏(详细思路+多语言题解)

华为非AI方向0603笔试第二题【爆破小游戏】,提供详细思路讲解以及代码实现。

qq_45776114的博客 358

【USACO4-3-2】街道赛跑Street Race bfs

//居然连最bfs都不会,果然我实在是太菜了啊qwq对于第一问我们遍历每个点,每次将这个点去掉进行bfs,如果最后到不了终点说明这个点就是不可避免的点。如果这个点是不可避免的点,我们的bfs实际上已经构建好了第一部分,接下来只要判断在第二部分中是否存在连到第一部分的边即可。如果没有的话就是中间路口,如果有的话说明在第二天存在一条从第一部分经过的路径,不满足题目条件。...

Mininda 472

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

P2752 [USACO4.3] 街道赛跑Street Race 题解

该题目要求分析一个街道跑道图,找出两类关键路口: 不可避免的路口:删去后无法从起点到达终点 中间路口:能将跑道分成两个良好部分的路口 解题思路: 使用BFS遍历图,检查每个点是否为必经点(删除后起点无法到达终点) 对必经点进一步验证是否为中间路口,确保分割后两部分无共同边 输出结果按升序排列 代码实现通过两次BFS分别判断必经点和中间路口,时间复杂度与图规模相关。

bz_2023f33的博客 807

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

usaco 4.3 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; p

志当存高远 757

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

USACO: Street Race

Street Race<br />IOI'95<br />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-w

374

usaco 4.3 Street Race 2010.8.7

Street Race IOI'95 Figure 1 gives an example of acourse 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; poin

Batman_的博客 377

USACO 4.3 Street Race (搜索)

2015-03-2620:55:52 思路:题目大意是给出一个有向图,给出起点和终点,让你求出两种点:   (1)任何从起点->终点的路径都必须经过的点。(2)该点将图分为两部分,(起点->该点)和(该点->终点)两个部分,且两个部分没有边相连。   考虑:显然(2)点必然是(1)点,所以我们先求(1)点,方法很简单,枚举删除每个点,如果删除后从起点出发走不到终...

dingdi3021的博客 129

USACO 4.3 Street Race

Street RaceIOI'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...

weixin_30483495的博客 144

USACO-Section 4.3 Street Race (枚举&&BFS)

第一问很好求,直接枚举然后判断是否能到达终点 第二问刚开始没理解题意,以为是当前点不在环上,最终明白应该是其作为起点时,不能到以其为终点的那一部分 又看了下题解,终于AC了。。。

idealism_xxm的专栏 478

Usaco Training Section 4.3 Street Race

题意真的挺绕的…… 第一问求从起点到终点所有路径上哪些点肯定出现,其实也就是把这些点连的边删掉,起点就到不了终点,枚举一下,大概是O(nm)的。 第二问求有哪些个点能把图分成两块,两块之间没有共同的边,只有那个分割点是公共的。我们可以发现,第二问答案的集合属于第一问的集合,因为如果第一问的条件不满足,第二问的条件也不会满足。于是我们把第一问的答案每个作为分割点,每次跑两个dfs,第一个从起点出...

Monster的博客 214

usaco training 4.3.3 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 rac

阿蒋的专栏 2027

Luogu P2752【usaco4.3.3】街道赛跑-Street Race(图论连通性)

题目描述 下图表示一次街道赛跑的跑道。可以看出有一些路口(用 0 到 N 的整数标号),和连接这些路口的箭头。路口 0 是跑道的起点,路口 N是跑道的终点。箭头表示单行道。运动员们可以顺着街道从一个路口移动到另一个路口(只能按照箭头所指的方向)。当运动员处于路口位置时,他可以选择任意一条由这个路口引出的街道。 上图:有 10 个路口的街道 一个良好的跑道具有如下几个特点: 每一个路口都可以由起点...

Admin-zx 的博客 1562

usaco 4.3 Street Race

 求有向图割点和分裂点,由于数据很小,直接枚举,去掉某点,然后判断即可,需要注意的地方是分裂点一定是割点 #include  #include  #include  #include  using namespace std;/*PROG: race3LANG: C++ID: heben991*/const int N = 110;i

Ben的专栏 745
上一篇: usaco4.3.1 Buy Low, Buy Lower
下一篇: usaco4.4.1 Shuttle Puzzle
max_kibble
博客等级 码龄11年 2粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值