1137. Bus Routes
Time limit: 1.0 second Memory limit: 64 MB
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 was possible to move from any stop to any other stop (probably making several transfers). The new mayor of the city decided to reform the city transportation system. He offered that there would be only one route going through all the sections where buses moved in the past. The direction of movement along the sections must be the same and no additional sections should be used.
Write a program, which creates one of the possible new routes or finds out that it is impossible.
Input
The first line of the input contains the number of old routes n. Each of the following n lines contains the description of one route: the number of stops m and the list of that stops. Bus stops are identified by positive integers not exceeding 10000. A route is represented as a sequence of m + 1 bus stop identifiers: l1, l2, …,lm, lm+1 = l1 that are sequentially visited by a bus moving along this route. A route may be self-intersected. A route always ends at the same stop where it starts (all the routes are 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 k and 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 | output |
---|---|
3 6 1 2 5 7 5 2 1 4 1 4 7 4 1 5 2 3 6 5 4 2 |
15 2 5 4 2 3 6 5 7 4 1 2 1 4 7 5 2 |
Hint
Here is a picture for the example:
Problem Source: Quarterfinal, Central region of Russia, Rybinsk, October 17-18 2001
***************************************************************************************
深搜,存储结构很重要。用vector存储有利于清空。
***************************************************************************************
1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<cstdio> 5 #include<cmath> 6 #include<vector> 7 using namespace std; 8 vector<int>adj[10003]; 9 vector<int>vis[10003]; 10 vector<int>ans; 11 int a[10003]; 12 int n,m,i,j,st; 13 void dfs(int x,int k)//深搜 14 { 15 for(int i=0;i<adj[x].size();i++) 16 if(!vis[x][i]) 17 { 18 ans.insert(ans.begin()+k,adj[x][i]);//压入值 19 vis[x][i]=true; 20 dfs(adj[x][i],k+1); 21 } 22 } 23 int main() 24 { 25 cin>>n; 26 int num=0; 27 int gs=-1; 28 while(n--) 29 { 30 cin>>m; 31 num+=m; 32 for(i=0;i<=m;i++) 33 { 34 cin>>a[i]; 35 if(gs==-1) 36 gs=a[i]; 37 if(i) 38 { 39 adj[a[i-1]].push_back(a[i]);//标记相邻的点 40 vis[a[i-1]].push_back(false);//初始化 41 } 42 } 43 } 44 ans.clear();//清空值 45 ans.push_back(gs); 46 dfs(gs,1); 47 if(num!=ans.size()-1)//欧拉回路不存在 48 { 49 cout<<'0'<<endl; 50 return 0; 51 } 52 cout<<num<<' '; 53 for(i=0;i<ans.size();i++)//输出深搜值 54 cout<<ans[i]<<' '; 55 cout<<endl; 56 return 0; 57 }