Z-bus RoutesTime
limit:1000MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i6 4u Submit Status Practice URAL 1137
Description
Several bus routes were in the city of Fishburg. None of the routes shared the same section of road, though common stops and intersections were possible. Fishburg Old residents stated that it is possible to move from any stop to any other stop (probably making several transf ERS). The new mayor of the city decided to reform, the city transportation system. He offered that there would is only one route going through all the sections where buses moved in the past. The direction of movement along the sections must is the same and no additional sections should be used. Write a program, which creates one of the possible new routes or finds off that it is impossible.
Input
The first line of the input contains the number of the old routes
N. Each of the following
NLines contains the description of one route:the number of stops
mAnd the list of that stops. Bus stops is identified by positive integers not exceeding 10000. A route is represented as a sequence of
m+ 1 bus Stop identifiers:
L1,
L2,...,
L
m,
L
m+1 =
L1 that is sequentially visited by a bus moving along this route. A Route may self-intersected. A route always ends at the same stop where it starts (all the routes is circular). The number of old routes:1≤
N≤100.
The number of stops:1≤
m≤1000.
The Number-identifier of the stop:1≤
L≤10000.
Output
The output contains the number of stops in the new route
kAnd the new route itself in the same format as in the input. The Last (
k+1)-th stop must be the same as the first. If It is impossible to make a new route according to the problem statement then write 0 (zero) to the output.
Sample Input
input |
Output |
36 1 2 5 7 5 2 14 1 4 7 4 15 2 3 6 5 4 2 |
15 2 5 4 2 3 6 5 7 4 1 2 1 4 7 5 2 |
Noteshere is a picture for the example: Test instructions will give you a few old routes, let you ask whether you can design a new route, can contain all the routes in this, DFS transform it, But note that when you return to plastic, do not empty the mark Vis.
#include <iostream>#include<cstdio>#include<algorithm>#include<vector>#include<queue>using namespacestd;#defineN 10010intN,m;vector<int>Ed[n];BOOLVis[n][n];intpa[100010],t;voidDfsintu) { inti; for(i =0; I < (int) Ed[u].size (); i++){ intv =Ed[u][i]; if(!Vis[u][v]) {Vis[u][v]=1; DFS (v); }} t++; Pa[t]=u; return ;}intMain () {inti,j,u,v; scanf ("%d",&N); intsum=0; for(i =1; I <= N; i++) {scanf ("%d",&m); Sum+=m; scanf ("%d",&u); for(j =1; J <= M; J + +) {scanf ("%d",&v); Ed[u].push_back (v); U=v; }} DFS (1); if(sum!=t-1) printf ("0\n"); Else{printf ("%d\n", T1); for(i = t; i >1; i--) printf ("%d", Pa[i]); printf ("%d\n", pa[1]); } return 0;}
URAL 1137Bus Routes (DFS)