URAL1137 Bus Routes
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: l 1, l 2, …, l m, l m+1 = l 1 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.
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.
Example
| 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 |
Notes
Here is a picture for the example:
__________________________________________________________________________________________________________
简单的求欧拉回路,套板子就好了,最后要判断一下存的点数量够不够,不够就输出0.
用了dfs求欧拉回路,fluery不会QWQ我是垃圾
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <stack> #include <queue> #include <map> #include <vector> #include <set> #include <deque> typedef long long ll; using namespace std; struct e { int next; int vis; }ee; vector<struct e> a[10005]; int p[100000]; int k=0,num=0; void dfs(int x) { int i; for (i=0;i<a[x].size();i++) { if (!a[x][i].vis) { a[x][i].vis=1; dfs(a[x][i].next); } } k++; p[k]=x; } int main() { int m,n,s; scanf("%d",&n); ee.vis=0; for (int i=0;i<n;i++) { scanf("%d%d",&m,&s); num+=m; for (int j=0;j<m;j++) { scanf("%d",&ee.next); a[s].push_back(ee); s=ee.next; } } dfs(1); if(num!=k-1) { printf("0\n"); } else { printf("%d ",num); for (int i=k;i>1;i--) { printf("%d ",p[i]); } printf("%d",p[1]); } }

浙公网安备 33010602011771号