#include #include //#include #include #include #define N 100000 using namespace std; int n, m; set g[N]; unsigned long long bs = 0, cs; int bestPath[N], currentPath[N], bl = 0, cl = 0; bool used[N]; clock_t start; const clock_t TC = 2.45 * CLOCKS_PER_SEC; void printBestPath(bool f) { ofstream out; out.open("maxpath.out"); /* for(int i = 0; i < n; ++i) { out << i+1 << ": "; for(set::iterator j = g[i].begin(); j != g[i].end(); ++j) { out << (*j)+1 << " "; } out << endl; } */ out << bl << endl; for(int i = bl-1; i >= 0; --i) { out << bestPath[i]+1 << endl; } out.close(); if(f) { exit(0); } } unsigned long long evalCurrentPath() { unsigned long long s = 0; for(int i = 0; i < cl; ++i) { s += (currentPath[i]+1) * (cl-i); } return s; } void DFS(int i) { //cout << "DFS: " << i+1 << endl; if((cs = evalCurrentPath()) > bs) { bs = cs; for(int j = 0; j < cl; ++j) { bestPath[j] = currentPath[j]; } bl = cl; } if(clock() - start >= TC) { printBestPath(1); } for(set::reverse_iterator it = g[i].rbegin(); it != g[i].rend(); ++it) { int current = *it; if(used[current]) { continue; } used[current] = 1; currentPath[cl++] = current; DFS(current);//cout << "cur: " << current+1 << endl; used[current] = 0; --cl; } } int main() { start = clock(); ifstream inp; inp.open("maxpath.in"); inp >> n >> m; int a1, a2; for(int i = 0; i < m; ++i) { inp >> a1 >> a2; g[a1-1].insert(a2-1); g[a2-1].insert(a1-1); } inp.close(); for(int k = n-1; k >= 0; --k) { for(int i = 0; i < n; ++i) { used[i] = 0; } used[k] = 1; currentPath[0] = k; cl = 1; DFS(k); //cout << endl; } printBestPath(0); //cout << "Elapsed time: " << (float)(clock() - start) / CLOCKS_PER_SEC << endl; return 0; }