USACO4.3.3 Street Race Problem Solving report

Source: Internet
Author: User
Tags printf
"Problem description"

The following figure 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. The arrows represent a single line of 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.

There are 10 intersections on the runway a good runway with the following features:




1), each intersection can be reached from the beginning.
2), from any intersection can reach the end.
3), the end point does not lead to any intersection.
4), athletes do not have to go through all the intersection to complete 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, at the same time the divided two parts meet the following conditions:


1), there is no common street between them

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. (Note: In short, it is said that the next day in addition to the middle intersection no matter how to run to the first day of the intersection)


Input format  
  
  include a good runway with a maximum of 50 intersections and 100 one-way streets. There is a total of n+2 line, the first line of the n+1 line in front of the street is numbered as a starting point (i-1), each number represents an end point. The end of the line ends with-2. The last line has only one number-1.  

Output format  
   
  Your program will have two lines of output: 
  The first line includes: The number of "unavoidable" intersections in the runway, followed by these intersections Ordinal, ordinal in ascending order.  
  The second line includes: the number of "intermediate junctions" in the runway followed by the serial numbers of these intersections, in ascending order of the numbers.  

Input sample   
   
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 sample"  
   
2 3 6
1 3

Data range  
   
  up to 50 intersections and 100 one-way streets.

Problem-solving ideas: According to test instructions, because the inevitable intersection from the beginning to the end must pass through the intersection, so the inevitable intersection can be enumerated in addition to the starting point and the end of each intersection, the intersection is deleted (false delete, with an array to mark), see from the beginning can go to the end, That is, a DFS (or BFS) traversal, if you can walk, then the intersection is not an unavoidable intersection, if not, then the junction is inevitable junction. Next is to seek the middle junction, because the middle intersection divides the runway into two parts, from the middle intersection and from the beginning to go through the intersection except the Middle junction is no public, and divided into two runways from the starting point after the intersection in the inevitable intersection has been marked, So simply enumerate each intersection except the starting point and the end point, starting at the intersection of a Dfs (or BFS) traversal, marking the intersection that can be reached, if the intersection from the starting point and the intersection from the intersection has the same, then the junction is not the middle junction, otherwise, the junction is the middle junction.


#include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <
Iostream> #include <vector> using namespace std;
const int maxn=55;
int n=0,x;
vector<int>g[maxn],ans1,ans2;
int VIS1[MAXN],VIS2[MAXN];
	void DFS1 (int i) {vis1[i]=1;
		for (int k=0;k<g[i].size (); k++) {int j=g[i][k];
		if (Vis1[j]) continue;
	DFS1 (j);
	}} void DFS2 (int i) {vis2[i]=1;
		for (int k=0;k<g[i].size (); k++) {int j=g[i][k];
		if (Vis2[j]) continue;
	DFS2 (j);
	}} int main () {freopen ("48.in", "R", stdin);
	Freopen ("48.out", "w", stdout);
		while (1) {scanf ("%d", &x);
		if (x==-1) break;
		else if (x==-2) n++;
		else {g[n].push_back (x);
	}} n--;  int cnt1=0,cnt2=0;
		Record answer number for (int i=1;i<n;i++) {memset (vis1,0,sizeof (VIS1));  Vis1[i]=1;
		Remove the DFS1 (0) from the intersection;
			if (vis1[n]==0)//judgment is unavoidable intersection {cnt1++;
		Ans1.push_back (i);
		} memset (Vis2,0,sizeof (VIS2));
		DFS2 (i);  int ok=1; Determine if the intermediate junction for (int j=0;j<=n;J + +) if (vis1[j]==1 && vis2[j]==1 && j!=i)//note j!=i, because in the false deletion, the vis1[i]=1, but from the starting point can not reach the first intersection {ok=0;
		Break
			} if (ok==1) {cnt2++;
		Ans2.push_back (i);
	}} printf ("%d", cnt1);
	for (int k=0;k<ans1.size (); k++) printf ("%d", ans1[k]);
	printf ("\ n");
	printf ("%d", cnt2);
	for (int k=0;k<ans2.size (); k++) printf ("%d", ans2[k]);
	printf ("\ n");
return 0; }


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.