Usaco Street Race

Source: Internet
Author: User

Figure one shows the runway of a street race. You can see that there are some intersections (integers labeled 0 to N), and arrows that connect these intersections. Junction 0 is the starting point of the runway, intersection N is the end of the runway. Arrows represent one-way streets. Athletes can move down the street from one intersection to another (only in the direction indicated by the arrows). When the athlete is at the intersection, he can choose any street from the intersection.

Figure one: The streets with 10 intersections

A good runway has the following features:

    • Every intersection can be reached from the beginning.
    • You can reach the finish line from any intersection.
    • The end point does not lead to any intersection.

Athletes don't have to go through all the intersections to finish the game. Some intersections are the choice of any route that must be reached (called "unavoidable"). In the example above, these intersections are 0,3,6,9. For the given good runway, your program must determine the "unavoidable" intersection of the set, excluding the starting and ending points.

Let's say the match takes two days. To achieve this goal, the original runway must be divided into two runways, each day using a runway. The first day, the starting point for the intersection 0, the end of a "middle junction"; The next day, the beginning is the middle junction, and the end of the intersection N. For the given good runway, your program needs to determine the "middle intersection" of the set. If the good runway C can be divided into two parts of the intersection, the two parts are good, and S is different from the starting point is different from the end point, while the divided two parts meet the following conditions: (1) They do not have a common street (2) s for their only common point, and S as one of the end points and another starting point. Then we call S "the Middle Junction". In the example, only junction 3 is the middle junction.

Data volume is small, direct violence DFS

/*id:modengd1prog:race3lang:c++*/#include <iostream> #include <stdio.h> #include <memory.h># Include <vector>using namespace Std;bool g[101][101];int color[101];vector<int> ans1;vector<int>    Ans2;bool dfs (int v,int m)//can go to the end {color[v]=1;    if (v==m) return true;  for (int i=0;i<=m;i++) {if (G[v][i]&&!color[i]) {if (Dfs (I,M)) return        True }} return false;}    BOOL DFS2 (int v,int m)//will not encounter a previously searched point {if (color[v]==1) return false;    color[v]=2;    for (int i=0;i<=m;i++) {if (G[V][I]&AMP;&AMP;COLOR[I]!=2&AMP;&AMP;!DFS2 (I,m)) return false; } return true;    int main () {freopen ("race3.in", "R", stdin);    Freopen ("Race3.out", "w", stdout);    int a,b,m;    m=0;    memset (g,false,sizeof (G));        for (int i=0;scanf ("%d", &a) &&a!=-1;i++) {if (a==-2) continue;        M=max (M,a);        G[i][a]=true; for (int j=0;scanF ("%d", &a) &&a!=-2;j++) {g[i][a]=true;        M=max (M,a);        }} for (int i=1;i<m;i++) {memset (color,0,sizeof (color));        Color[i]=1;            if (!dfs (0,m)) {ans1.push_back (i);            color[i]=0;        if (DFS2 (i,m)) ans2.push_back (i);    }} cout<<ans1.size ();    for (int i=0;i<ans1.size (); i++) cout<< "<<ans1[i];    cout<<endl;    Cout<<ans2.size ();    for (int i=0;i<ans2.size (); i++) cout<< "<<ans2[i];    cout<<endl; return 0;}

  

Usaco Street Race

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.