1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #include <cstdlib> #include <cmath> #include <algorithm> #include <cstring> #include <vector> #include <queue> #include <map> #include <cstdio> #include <iostream>
struct Edge { int u, v; bool w;
Edge(int U = 0, int V = 0, bool W = true) { (*this).u = U, (*this).v = V, (*this).w = W; }
void reverse() { std::swap((*this).u, (*this).v); return; }
Edge getReversed() { return Edge((*this).v, (*this).u, (*this).w); }
Edge operator = (const Edge& inEdge) { (*this).u = inEdge.u, (*this).v = inEdge.v, (*this).w = inEdge.w; return *this; } } emptyEdge;
inline bool cmp(Edge E, Edge e) { return E.v < e.v; }
inline void copy(int n, int* ori, int* des) { for(int i = 1; i <= n; i++) des[i] = ori[i]; }
inline void set(int n, bool stat, bool* des) { for(int i = 1; i <= n; i++) des[i] = stat; }
inline bool isMinDic(int n, int* dicOri, int* dicCmp) { for(int i = 1; i <= n; i++) { if(dicCmp[i] != dicOri[i]) { if(dicCmp[i] > dicOri[i]) return false; if(dicCmp[i] < dicOri[i]) return true; } } return false; }
inline void setEdge(int u, int v, bool w, std::vector<Edge>* graph) { for(std::vector<Edge>::iterator it = graph[u].begin(); it != graph[u].end(); it++) { if((*it).v == v) { (*it).w = w; return; } } }
void DFS(int now, int& depth, bool* isVisited, std::vector<Edge>* graph, int* dic, int* cmpDic, bool& wasEqual, bool& isBestSolution, bool& notBestSolution) { if(notBestSolution) return; isVisited[now] = true; dic[depth] = now; if(wasEqual && !isBestSolution) { dic[depth] != cmpDic[depth] ? (dic[depth] < cmpDic[depth] ? isBestSolution = true : false), (dic[depth] > cmpDic[depth] ? notBestSolution = true : false), wasEqual = false : wasEqual = true; } depth++; for(std::vector<Edge>::iterator it = graph[now].begin(); it != graph[now].end(); it++) { if(!isVisited[(*it).v] && (*it).w) DFS((*it).v, depth, isVisited, graph, dic, cmpDic, wasEqual, isBestSolution, notBestSolution); } }
inline void subtask(int n, int& depth, bool* isVisited, std::vector<Edge>* graph, int* dic, int* resultDic, bool& wasEqual, bool& isBestSolution, bool& notBestSolution) { for(int k = 1;k <= n;k++) { for(std::vector<Edge>::iterator it = graph[k].begin(); it != graph[k].end(); it++) { (*it).w = false;
int tDic[1 + n]; tDic[0] = 0, depth = 1, wasEqual = true, isBestSolution = false, notBestSolution = false; for(int i = 1; i <= n; i++) {tDic[i] = 5000 + 1; isVisited[i] = false;}
DFS(1, depth, isVisited, graph, tDic, resultDic, wasEqual, isBestSolution, notBestSolution); if(isBestSolution) copy(n, tDic, resultDic);
(*it).w = true; } } }
int main() { int n, m; std::cin >> n >> m; std::vector<Edge> graph[1 + n]; for(int i = 1; i <= m; i++) { int u, v; scanf("%i %i", &u, &v); graph[u].push_back(Edge(u, v)); graph[v].push_back(Edge(v, u)); } for(int i = 1; i <= n; i++) { std::sort(graph[i].begin(), graph[i].end(), cmp); }
int dic[1 + n]; for(int i = 1;i <= n;i++){dic[i] = 5000 + 1;} dic[0] = 0; bool isVisited[1 + n]; memset(isVisited, false, sizeof(isVisited)); int depth = 1; bool wasEqual = true, isBestSolution = false, notBestSolution = false; n != m ? DFS(1, depth, isVisited, graph, dic, dic, wasEqual, isBestSolution, notBestSolution) : subtask(n, depth, isVisited, graph, dic, dic, wasEqual, isBestSolution, notBestSolution);
for(int i = 1; i <= n; i++) { printf("%i ", dic[i]); } return 0; }
|