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
—————————————————————————————————题解
对于第一问,枚举每个点,从第一个点暴搜,如果不经过这个点能否到达终点,不能到达就是解
第二问的解是第一问的子集,枚举第一问中求得的每个点,从第一个点和这个点同时暴搜,看有没有两个点都能到达的点,没有的话就是解
 /*
ID: ivorysi
LANG: C++
TASK: race3
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x7fffffff
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define MAXN 30005
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
struct node {
int to,next;
}edge[];
int head[],sumedge;
void add(int u,int v) {
edge[++sumedge].to=v;
edge[sumedge].next=head[u];
head[u]=sumedge;
} int vis1[],vis2[],cnt;
queue<int> q;
void bfs(int u,int *w,int p) {
while(!q.empty()) {q.pop();}
q.push(u);
siji(i,,cnt) w[i]=;
while(!q.empty()) {
int now=q.front();q.pop();
w[now]=;
for(int i=head[now];i;i=edge[i].next) {
int v=edge[i].to;
if(v!=p && w[v]==) q.push(v);
}
}
}
void init() {
cnt=;
int u;
while() {
scanf("%d",&u);
if(u==-) ++cnt;
else if(u==-) break;
else {
add(cnt,u+);
}
}
}
vector<int> ans1,ans2;
void solve() {
init();
--cnt;
siji(i,,cnt-) {
bfs(,vis1,i);
if(vis1[cnt]==) ans1.push_back(i);
}
printf("%d",ans1.size());
xiaosiji(i,,ans1.size()) {
printf(" %d",ans1[i]-);
}
puts("");
xiaosiji(i,,ans1.size()) {
bfs(,vis1,ans1[i]);
bfs(ans1[i],vis2,);
bool flag=;
siji(j,,cnt) {
if(vis1[j] && vis2[j]) {flag=;break;}
}
if(flag) {
ans2.push_back(ans1[i]);
}
}
printf("%d",ans2.size());
xiaosiji(i,,ans2.size()) {
printf(" %d",ans2[i]-);
}
puts("");
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("race3.in","r",stdin);
freopen("race3.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
 

USACO 4.3 Street Race的更多相关文章

  1. USACO Section 4.3 Street Race(图的连通性+枚举)

    虽说是IOI'95,但是也是挺水的..for 第一问,n最大为50,所以可以直接枚举起点和终点之外的所有点,然后dfs判断是否连通:for 第二问,易知答案一定是第一问的子集,所以从第一问中的答案中枚 ...

  2. USACO4.3 Street Race【分析】

    这道题,感觉不是很难,分析清楚之后非常简单.(标签都不知道怎么加) 读完题首先想到了分割点一定是必经点的一种特殊情况,如果分割点不是必经点的话,那么它就不能把这个图分成两半(存在不经过它的边沟通两半) ...

  3. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  4. USACO Section 4

    前言 好久没更新这个系列了,最近闲的无聊写一下.有两题搜索懒得写了. P2737 [USACO4.1]麦香牛块Beef McNuggets https://www.luogu.com.cn/probl ...

  5. USACO 6.1 Postal Vans(一道神奇的dp)

    Postal Vans ACM South Pacific Region -- 2003 Tiring of their idyllic fields, the cows have moved to ...

  6. USACO . Your Ride Is Here

    Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...

  7. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  8. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  9. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

随机推荐

  1. python---函数补充(变量传递),语句执行顺序(入栈顺序)

    一:函数补充 默认作为函数参数的数据,是浅拷贝传递.不是和C等语言一样,产生一个临时变量. class T: def __init__(self,num): print(id(num)) self.n ...

  2. MySQL报错】ERROR 1558 (HY000): Column count of mysql.user is wrong. Expected 43, found 39.

    之前在centos6.4系统安装的是自带的mysql 5.1版本,后来升级到了5.6版本,执行以下命令报错 在网上查找原因说说因为升级不当导致,执行以下命令即可正常执行命令 mysql_upgrade ...

  3. 2017 清北济南考前刷题Day 5 morning

    期望得分:100+100+0=200 实际得分: 坐标的每一位不是0就是1,所以答案就是 C(n,k) #include<cstdio> #include<iostream> ...

  4. 七、新时间日期 API

    一.使用 LocalDate. LocalTime. LocalDateTime LocalDate. LocalTime. LocalDateTime 类的实例是不可变的对象,分别表示使用 ISO- ...

  5. Java并发编程原理与实战四十五:问题定位总结

    背景   “线下没问题的”. “代码不可能有问题 是系统原因”.“能在线上远程debug么”    线上问题不同于开发期间的bug,与运行时环境.压力.并发情况.具体的业务相关.对于线上的问题利用线上 ...

  6. 转 -- OK6410 tftp下载内核、文件系统以及nand flash地址相关整理、总结

    转载地址:http://emouse.cnblogs.com/ 飞凌官方提供了一键下载烧写linux的方式,相对来说比较方便,但是对于开发来说不够灵活,因此这篇文章把tftp相关的点介绍一下,整理下其 ...

  7. 训练赛第一场D题

    解题报告:一开始不知道ATA的意思,后来才知道原来是转置矩阵乘以原来的矩阵.这题说起来比较麻烦就不说了,直接上代码: #include<cstdio> #include<cstrin ...

  8. C++传递二维数字给一个自定义函数

    如果参数是多维数组,那么参数必须指明第一维意外得所有未得长度:比如你的 void tt(char a[][20])或者 void tt(char (*a)[20]) 另外这样也是可以的char *a[ ...

  9. POJ 1128 Frame Stacking (拓扑排序)

    题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...

  10. E - Sudoku HDU - 5547 (搜索+暴力)

    题目链接:https://cn.vjudge.net/problem/HDU-5547 具体思路:对于每一位上,我们可以从1到4挨着去试, 具体判断这一位可不可以的时候,看当前这一位上的行和列有没有冲 ...