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. select()函数

    select(),确定一个或多个套接口的状态,本函数用于确定一个或多个套接口的状态,对每一个套接口,调用者可查询它的可读性.可写性及错误状态信息,用fd_set结构来表示一组等待检查的套接口,在调用返 ...

  2. bzoj千题计划166:bzoj2179: FFT快速傅立叶

    http://www.lydsy.com/JudgeOnline/problem.php?id=2179 FFT做高精乘 #include<cmath> #include<cstdi ...

  3. springboot中@webfilter注解的filter时注入bean都是null

    在使用@Webfilter注解Filter的情况下,不上外部tomcat时是没有问题的.但是在tomcat下运行时,filter中注入的bean就都是null 解决办法: 一:去掉@Webfilter ...

  4. python3 flask部署新浪sae

    第一步需要注册新浪云 https://sae.sinacloud.com/ 第二步 创建应用,注:直接选python2.7不用纠结,后面可以指定python版本,共享版 第三步 找到git仓库信息 第 ...

  5. PyQt4 里的表格部件的使用方法: QTableWidget

    PyQt4 里的表格部件的使用方法: QTableWidget QT下QTableWidget使用方法小结 - - 博客频道 - CSDN.NET http://blog.csdn.net/jingz ...

  6. Throwable、Error、Exception、RuntimeException 区别

    1.java将所有的错误封装为一个对象,其根本父类为Throwable, Throwable有两个子类:Error和Exception. 2.Error是Throwable 的子类,用于指示合理的应用 ...

  7. 小程序web-view wx.miniProgram.postMessage 坑记录

    web-view吧,其实微信官方应该是非常不支持在小程序上嵌套web的,它希望你直接用小程序上的代码,而放弃web的实现,当然,也是为了防止用小程序去嵌套别的广告页面.所以官方对web-view的操作 ...

  8. Anaconda+django写出第一个web app(五)

    今天开始学习网页风格和设计,就像python有Web框架一样,也有一些CSS框架.对于CSS框架,我们可以使用默认的样式,也可以在原基础上编辑修改.本教程使用的是materialize这个CSS框架[ ...

  9. HDU 1074 Doing Homework (dp+状态压缩)

    题目链接 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot ...

  10. Rico Board.1.环境配置

    1.搭建开发环境 1.解压文件 sudo tar -jvxf gcc-linaro-arm-linux-gnueabihf-4.7-2013.03-20130313_liunx.tar.bz2 -C ...