Usaco section 4.3 Street Race (connectivity + enumeration of graphs)

Source: Internet
Author: User

Although Ioi ' 95, but also very water. For the first question, the n maximum is 50, so that all points outside the start and end point can be enumerated directly, and then DFS determines whether the connection is connected; For the second question, the easy-to-know answer must be a subset of the first question, so enumerating from the answers in the first question is also judged by DFS.

#include <cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<vector>#defineRep (i,r) for (int i=0;i<r;i++)#defineCLR (x,c) memset (x,c,sizeof (x))using namespacestd;Const intmaxn= -+5;intn=0;intVis[maxn];vector<int>G[MAXN];voidRead () { for(;;) {        intT,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++; }}    intDfsinti) {vis[i]=1; Rep (J,g[i].size ()) {intt=G[i][j]; if(t==n-1)return 1; if(Vis[t])Continue; if(Dfs (t))return 1; }    return 0;}voidDFS (inti) {vis[i]=1; Rep (J,g[i].size ()) {intt=G[i][j]; if(Vis[t])Continue;    DFS (t); }}        intDfsjudge (inti) {vis[i]=2; Rep (J,g[i].size ()) {intt=G[i][j]; if(vis[t]==1)return 1; if(Vis[t])Continue; if(Dfsjudge (t))return 1; }    return 0;}voidWork () {vector<int>ans;    Ans.clear ();  for(intI=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;}intMain () {Freopen ("race3.in","R", stdin); Freopen ("Race3.out","W", stdout);        Read ();                Work (); return 0;}
View Code Street Race
IOI ' 95

Figure 1 gives the 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 could choose any outgoing arrow.


Figure 1: A Street course with ten points

A Well-Formed course has the following properties:

    • Every point in the course can is reached from the start.
    • The finish can reached from the course.
    • The finish has no outgoing arrows.

A participant does not has to visit every point of the course to reach the finish. Some points, however, is unavoidable. In the example, these is points 0, 3, 6, and 9. Given A well-formed course, your program must determine the set of unavoidable points that all participants has to visit, Excluding start and finish.

Suppose the race have to is held on both consecutive days. For that purpose the course have to is split into a courses, one for each day. On the first day, the start was at point 0 and the finish at some ' splitting point '. On the second day, the start was at the-splitting point and the-the-finish is at point N. Given A well-formed course, your program must also determine the set of splitting points. A point S are a splitting point to the well-formed course C if S differs from the star T and the finish of C, and the Cour Se can is split into the well-formed courses that (1) has no common arrows and (2) has S as their only common point, wit H 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:race3input FORMAT

The input file contains a well-formed course with at most of the points and at most of the arrows. There is 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-23-23-25 4-26 4-26-27 8-29-25 9-2-2-1
OUTPUT FORMAT

Your program should write, 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 poi NTS, in ascending order.

SAMPLE OUTPUT (file race3.out)
2 3 61 3

Usaco section 4.3 Street Race (connectivity + enumeration of graphs)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.