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

题意:给你一张有向图,要你找出一些从0到n必须经过的点,找到令一些点,这些点可以把原图划分为两个满足条件的图,两个图之间不能有边相连。。。

分析:第一个解比较简单,由于数据范围小,之间枚举去掉所有点,遍历一下图,看能否到达n就行,第二个解也不难,仔细考虑之后,我们发现,这些点可定属于第一类点,之间枚举去第一解里的点,然后从0遍历标上1,然后加上去掉的点,从去掉的点出发标号2,如果遇到标号1的说明不是要求的点。。。

代码:

/*
ID: 15114582
PROG: race3
LANG: C++
*/
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int mm=222;
int head[mm],ver[mm],next[mm],vis[mm],out1[mm],out2[mm];
int n,edge,ans1,ans2;
bool ok(int u)
{
    if(u==n)return 1;
    vis[u]=1;
    for(int i=head[u],v;i>=0;i=next[i])
        if(!vis[v=ver[i]])if(ok(v))return 1;
    return 0;
}
bool check(int u)
{
    vis[u]=2;
    for(int i=head[u],v;i>=0;i=next[i])
        if(!vis[v=ver[i]])
        {
            if(check(v))return 1;
        }
        else if(vis[v]==1)return 1;
    return 0;
}
int main()
{
    freopen("race3.in","r",stdin);
    freopen("race3.out","w",stdout);
    int i,j;
    memset(head,-1,sizeof(head));
    ans1=ans2=n=edge=0;
    while(1)
    {
        while(scanf("%d",&j),j>-1)
            ver[edge]=j,next[edge]=head[n],head[n]=edge++;
        if(j<-1)++n;
        else break;
    }
    --n;
    for(i=1;i<n;++i)
    {
        memset(vis,0,sizeof(vis));
        vis[i]=1;
        if(!ok(0))out1[ans1++]=i;
    }
    for(i=0;i<ans1;++i)
    {
        memset(vis,0,sizeof(vis));
        vis[out1[i]]=2;
        ok(0);
        if(!check(out1[i]))out2[ans2++]=out1[i];
    }
    printf("%d",ans1);
    for(i=0;i<ans1;++i)printf(" %d",out1[i]);puts("");
    printf("%d",ans2);
    for(i=0;i<ans2;++i)printf(" %d",out2[i]);puts("");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值