zoukankan      html  css  js  c++  java
  • 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
    —————————————————————————————————题解
    对于第一问,枚举每个点,从第一个点暴搜,如果不经过这个点能否到达终点,不能到达就是解
    第二问的解是第一问的子集,枚举第一问中求得的每个点,从第一个点和这个点同时暴搜,看有没有两个点都能到达的点,没有的话就是解
      1 /*
      2 ID: ivorysi
      3 LANG: C++
      4 TASK: race3
      5 */
      6 #include <iostream>
      7 #include <cstdio>
      8 #include <cstring>
      9 #include <algorithm>
     10 #include <queue>
     11 #include <set>
     12 #include <vector>
     13 #include <string.h>
     14 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
     15 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
     16 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
     17 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
     18 #define inf 0x7fffffff
     19 #define ivorysi
     20 #define mo 97797977
     21 #define hash 974711
     22 #define base 47
     23 #define MAXN 30005
     24 #define fi first
     25 #define se second
     26 #define pii pair<int,int>
     27 using namespace std;
     28 struct node {
     29     int to,next;
     30 }edge[205];
     31 int head[205],sumedge;
     32 void add(int u,int v) {
     33     edge[++sumedge].to=v;
     34     edge[sumedge].next=head[u];
     35     head[u]=sumedge;
     36 }
     37 
     38 int vis1[55],vis2[55],cnt;
     39 queue<int> q;
     40 void bfs(int u,int *w,int p) {
     41     while(!q.empty()) {q.pop();}
     42     q.push(u);
     43     siji(i,1,cnt) w[i]=0;
     44     while(!q.empty()) {
     45         int now=q.front();q.pop();
     46         w[now]=1;
     47         for(int i=head[now];i;i=edge[i].next) {
     48             int v=edge[i].to;
     49             if(v!=p && w[v]==0) q.push(v);
     50         }
     51     }
     52 }
     53 void init() {
     54     cnt=1;
     55     int u;
     56     while(1) {
     57         scanf("%d",&u);
     58         if(u==-2) ++cnt;
     59         else if(u==-1) break;
     60         else {
     61             add(cnt,u+1);
     62         }
     63     }
     64 }
     65 vector<int> ans1,ans2;
     66 void solve() {
     67     init();
     68     --cnt;
     69     siji(i,2,cnt-1) {
     70         bfs(1,vis1,i);
     71         if(vis1[cnt]==0) ans1.push_back(i);
     72     }
     73     printf("%d",ans1.size());
     74     xiaosiji(i,0,ans1.size()) {
     75         printf(" %d",ans1[i]-1);
     76     }
     77     puts("");
     78     xiaosiji(i,0,ans1.size()) {
     79         bfs(1,vis1,ans1[i]);
     80         bfs(ans1[i],vis2,0);
     81         bool flag=1;
     82         siji(j,1,cnt) {
     83             if(vis1[j] && vis2[j]) {flag=0;break;}
     84         }
     85         if(flag) {
     86             ans2.push_back(ans1[i]);
     87         }
     88     }
     89     printf("%d",ans2.size());
     90     xiaosiji(i,0,ans2.size()) {
     91         printf(" %d",ans2[i]-1);
     92     }
     93     puts("");
     94 }
     95 int main(int argc, char const *argv[])
     96 {
     97 #ifdef ivorysi
     98     freopen("race3.in","r",stdin);
     99     freopen("race3.out","w",stdout);
    100 #else
    101     freopen("f1.in","r",stdin);
    102 #endif
    103     solve();
    104     return 0;
    105 }
     
  • 相关阅读:
    数据系统与分布式(二) 分布式数据系统(复制与分片)
    数据系统和分布式(一)数据系统基础
    可执行文件(ELF)的装载与进程
    HTTPS协议
    后台开发 缓存, 数据库, 高并发等等
    Golang中new和make的区别
    吴恩达:机器学习里面的作业1遇到的
    笔记——操作系统导论:环境配置
    笔记——操作系统导论:第二章
    Games 101 作业1代码解析
  • 原文地址:https://www.cnblogs.com/ivorysi/p/6360347.html
Copyright © 2011-2022 走看看