插入回路
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 const unsigned MaxJunc = 45; 5 const unsigned MaxStreet = 1995; 6 7 unsigned graph[MaxJunc][MaxStreet]; 8 unsigned stack[MaxStreet]; 9 unsigned juncDegree[MaxJunc]; 10 bool hasVisited[MaxStreet]; 11 unsigned top, maxStreetNo; 12 13 void Euler(int s) 14 { 15 int i; 16 for (i = 1; i <= maxStreetNo; i++){ 17 if (graph[s][i] && !hasVisited[i]){ 18 hasVisited[i] = true; 19 Euler(graph[s][i]); 20 stack[top++] = i; 21 22 } 23 } 24 } 25 26 int main() 27 { 28 unsigned home; 29 unsigned x, y, z; 30 int i; 31 while (1) 32 { 33 memset(graph, 0, sizeof(unsigned)*MaxJunc*MaxStreet); 34 memset(stack, 0, sizeof(unsigned)*MaxStreet); 35 memset(juncDegree, 0, sizeof(unsigned)*MaxJunc); 36 memset(hasVisited, false, sizeof(bool)*MaxStreet); 37 maxStreetNo = 0; 38 top = 0; 39 40 cin >> x >> y; 41 home = min(x, y); 42 if (x == 0 && y == 0) 43 break; 44 while (x != 0 && y != 0) 45 { 46 cin >> z; 47 graph[x][z] = y; 48 juncDegree[x]++; 49 graph[y][z] = x; 50 juncDegree[y]++; 51 maxStreetNo = max(maxStreetNo, z); 52 cin > x >> y; 53 } 54 for (i = 1; i < MaxJunc;++i) 55 if (juncDegree[i] % 2) 56 break; 57 if (i < MaxJunc) 58 { 59 cout << "Round trip does not exit." << endl; 60 } 61 else 62 { 63 Euler(home); 64 for (i = top - 1; i >= 0; i--) 65 cout << stack[i] << " "; 66 cout << endl; 67 } 68 } 69 return 0; 70 }