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

----------------------------------------------------------------------

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#define rep(i,r) for(int i=0;i<r;i++)
#define clr(x,c) memset(x,c,sizeof(x))
using namespace std;
const int maxn=50+5;
int n=0;
int vis[maxn];
vector<int> g[maxn];
void read() {    
    for(;;) {
        int t,pd=0;
        g[n].clear();
        while(scanf("%d",&t) && t!=-2) {
            if(t==-1) { pd=1; break; }
            g[n].push_back(t);
        }
        if(pd) break;    
        n++;
    }
}
    
int dfs(int i) {
    vis[i]=1;
    rep(j,g[i].size()) {
        int t=g[i][j];
        if(t==n-1) return 1;
        if(vis[t]) continue;
        if(dfs(t)) return 1;
    }
    return 0;
}
void DFS(int i) {
    vis[i]=1;
    rep(j,g[i].size()) {
        int t=g[i][j];
        if(vis[t]) continue;
        DFS(t);
    }
}
        
int dfsJudge(int i) {
    vis[i]=2;
    rep(j,g[i].size()) {
        int t=g[i][j];
        if(vis[t]==1) return 1;
        if(vis[t]) continue;
        if(dfsJudge(t)) return 1;
    }
    return 0;
}
void work() {
    
    vector<int> ans;
    ans.clear();
    for(int i=1;i<n-1;i++) {
        clr(vis,0);
        vis[i]=1;
        if(!dfs(0)) ans.push_back(i);
    }
        
    cout<<ans.size();
    rep(i,ans.size()) cout<<' '<<ans[i];
    cout<<endl;
    
    vector<int> ans2;
    ans2.clear();
    rep(i,ans.size()) {
         clr(vis,0);
         vis[ans[i]]=2;
         DFS(0);
         if(!dfsJudge(ans[i])) ans2.push_back(ans[i]);
    }
    
    printf("%d",ans2.size());
    rep(i,ans2.size()) printf(" %d",ans2[i]);
    cout<<endl;
}
int main()
{
    freopen("race3.in","r",stdin);
    freopen("race3.out","w",stdout);
    
    read();
    
    work();
        
    
    return 0;
}

----------------------------------------------------------------------

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

USACO Section 4.3 Street Race(图的连通性+枚举)的更多相关文章

  1. USACO 4.3 Street Race

    Street RaceIOI'95 Figure 1 gives an example of a course for a street race. You see some points, labe ...

  2. USACO Section 4

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

  3. 数据结构-图-Java实现:有向图 图存储(邻接矩阵),最小生成树,广度深度遍历,图的连通性,最短路径1

    import java.util.ArrayList; import java.util.List; // 模块E public class AdjMatrixGraph<E> { pro ...

  4. Victoria的舞会2——图的连通性及连通分量

    [Vijos1022]]Victoria的舞会2 Description Victoria是一位颇有成就的艺术家,他因油画作品<我爱北京天安门>闻名于世界.现在,他为了报答帮助他的同行们, ...

  5. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

  6. poj 3310(并查集判环,图的连通性,树上最长直径路径标记)

    题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...

  7. POJ2513(字典树+图的连通性判断)

    //用map映射TLE,字典树就AC了#include"cstdio" #include"set" using namespace std; ; ;//26个小 ...

  8. 图的连通性问题的小结 (双连通、2-SAT)

    图的连通性问题包括: 1.强连通分量. 2.最小点基和最小权点基. 3.双连通. 4.全局最小割. 5.2-SAT 一.强连通分量 强连通分量很少单独出题,一般都是把求强连通分量作为缩点工具. 有三种 ...

  9. 2018年牛客多校寒假 第四场 F (call to your teacher) (图的连通性)

    题目链接 传送门:https://ac.nowcoder.com/acm/contest/76/F 思路: 题目的意思就是判断图的连通性可以用可达性矩阵来求,至于图的存储可以用邻接矩阵来储存,求出来可 ...

随机推荐

  1. Android-Tab单选控件

    今天看到项目中有一个控件写得很美丽,据说是github上开源的控件,地址没找到,例如以下图所看到的,很常见的效果,几个tab页面来回切换: 转载请标明出处:http://blog.csdn.net/g ...

  2. 使用Vitamio打造自己的Android万能播放器(3)——本地播放(主界面、播放列表)

    前言 打造一款完整可用的Android播放器有许多功能和细节需要完成,也涉及到各种丰富的知识和内容,本章将结合Fragment.ViewPager来搭建播放器的主界面,并实现本地播放基本功能.系列文章 ...

  3. java中文乱码解决之道(三)—–编码详情:伟大的创想—Unicode编码

    原文出处:http://cmsblogs.com/?p=1458 随着计算机的发展.普及,世界各国为了适应本国的语言和字符都会自己设计一套自己的编码风格,正是由于这种乱,导致存在很多种编码方式,以至于 ...

  4. CentOS 7解决Local Time与实际时间相差8小时问题

    通过date -s “2014-12-06 15:00:00”以及timedatectl set-time “2014-12-06 15:00:00” ,以及ntp等方式均知识临时有效,苦恼了我半天. ...

  5. 获取UIButton的一些属性

    获取文字  button.currentTitle 更多如下: @property(nullable, nonatomic,readonly,strong) NSString *currentTitl ...

  6. HttpServletRequest 各种方法总结(转自百度经验)

    HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象的方法,可以获得客户这些信息. req ...

  7. git SSh key多个key对应多个项目

    必看 1. 本文不教你怎么生成key,主要解决多个项目对应多个SSH KEY的问题,在csdn code库上遇到的人估计很苦恼,为什么多个项目不能用一个key,为什么添加相同的key就会报重复 2. ...

  8. Qt 之 show,hide,setVisible,setHidden,close 等小结

    0QObject::deleteLater()delete obj;析构对象1QWidget::setVisible(bool)使得Widget可见或不可见2QWidget::setHidden(bo ...

  9. linux开机自动挂载NTFS-WINDOWS分区

    1.安装ntfs-3g-2009.4.4.tgz 2.输入fdisk -l 看一下分区 由此可见:/dev/sda5,6,7 即是windows下的D,E,F盘(NTFS格式). 3.vim /etc ...

  10. Python Tensorflow下的Word2Vec代码解释

    前言: 作为一个深度学习的重度狂热者,在学习了各项理论后一直想通过项目练手来学习深度学习的框架以及结构用在实战中的知识.心愿是好的,但机会却不好找.最近刚好有个项目,借此机会练手的过程中,我发现其实各 ...