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; 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的更多相关文章
- USACO Section 4.3 Street Race(图的连通性+枚举)
虽说是IOI'95,但是也是挺水的..for 第一问,n最大为50,所以可以直接枚举起点和终点之外的所有点,然后dfs判断是否连通:for 第二问,易知答案一定是第一问的子集,所以从第一问中的答案中枚 ...
- USACO4.3 Street Race【分析】
这道题,感觉不是很难,分析清楚之后非常简单.(标签都不知道怎么加) 读完题首先想到了分割点一定是必经点的一种特殊情况,如果分割点不是必经点的话,那么它就不能把这个图分成两半(存在不经过它的边沟通两半) ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- USACO Section 4
前言 好久没更新这个系列了,最近闲的无聊写一下.有两题搜索懒得写了. P2737 [USACO4.1]麦香牛块Beef McNuggets https://www.luogu.com.cn/probl ...
- USACO 6.1 Postal Vans(一道神奇的dp)
Postal Vans ACM South Pacific Region -- 2003 Tiring of their idyllic fields, the cows have moved to ...
- 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 ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
随机推荐
- HTML常用标签-<head>内常用标签
HTML常用标签-<head>内常用标签 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML是什么 1>.超文本标记语言(Hypertext Ma ...
- 视音频数据处理入门:FLV封装格式解析
===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...
- css选择器 nth-child
html代码: <div> <p>多云转晴</p> <p>多云转晴</p> <p>多云转晴</p> <p> ...
- Web应用开发中的几个问题
Introduction 由于Ajax技术在Gmail中的成功应用和高性能的V8引擎的推出使得编写Web应用变得流行 起来,使用前端技术也可以编写具有复杂交互的应用.相对于native应用,Web应用 ...
- 基本控件文档-UIView属性
CHENYILONG Blog 基本控件文档-UIView属性 Fullscreen UIView属性技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http ...
- MySQL中JSON字段的使用技巧
mysql5.7.8之后开始原生支持json. 在类似mongodb这种nosql数据库中,json存储数据是非常自然的, 在mysql中合理的使用json,能够带来极大的便利 Json字段的使用场景 ...
- jquery对不同id的按钮执行同一类型的操作
不同id执行相同操作: $("#id1,#id2,#id3,#id4") 获取相同class的text值: $(".className").each(funct ...
- [转]closed-form solution (闭合解/解析解)和数值解的理解
参考整理自:http://hi.baidu.com/cjb366/item/7290773b2d2eb9f2a9842873 closed-form solution :一般翻译为闭合解/解析解.这一 ...
- Max Sum Plus Plus (动态规划) HDU1024
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1024 (http://www.fjutacm.com/Problem.jsp?pid=1375) 题意 ...
- springCloud全实战超详细代码demo+笔记
码云: https://gitee.com/houzheng1216/springcloud