// #define HOME #include #include #include #include #include #include using namespace std; typedef vector > nei_list; namespace greedy_sol { int seed=827289372; bool used[100005]; vector solve_greedy(int n, const nei_list & nei); void find_ans(const nei_list & nei, int u, vector & res); } namespace rand_dfs_sol { int seed=24632; bool used[100005]; vector solve_rand_dfs(int n, const nei_list & nei); void rand_dfs(const nei_list & nei, int u, vector & res); } vector solve(int n, const nei_list & nei); void improve(vector & a, const vector & oth); long long find_val(const vector &); void print_ans(const vector &); int main() { #ifndef HOME ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); freopen("maxpath.in", "r", stdin); freopen("maxpath.out", "w", stdout); #endif nei_list nei; int n, m; cin>>n>>m; nei.resize(n+1); for (int i=0; i>u>>v; nei[u].push_back(v); nei[v].push_back(u); } print_ans(solve(n, nei)); char I; cin >> I; return 0; } vector solve(int n, const nei_list & nei) { vector ans; if (n<=1000) { greedy_sol::seed=724323378; improve(ans, greedy_sol::solve_greedy(n, nei)); } /* int seeds[21]={}; for (int i=0; i<21; i++) { rand_dfs_sol::seed=seeds[i]; improve(ans, rand_dfs_sol::solve_rand_dfs(n, nei)); } */ improve(ans, rand_dfs_sol::solve_rand_dfs(n, nei)); return ans; } vector rand_dfs_sol::solve_rand_dfs(int n, const nei_list & nei) { srand(time(NULL)); /* srand(seed); */ int iter_num=700; vector ans; while (iter_num--) { int u=rand()%n+1; vector cur_ans; rand_dfs(nei, u, cur_ans); improve(ans, cur_ans); reverse(cur_ans.begin(), cur_ans.end()); improve(ans, cur_ans); } return ans; } void rand_dfs_sol::rand_dfs(const nei_list & nei, int u, vector & res) { used[u]=true; vector unused_nei; for (int i=0; i greedy_sol::solve_greedy(int n, const nei_list & nei) { srand(seed); long long max_val=0; vector ans; int it=0; for (int u=n; it<=1500; (it<=1000?u--:u=rand()%n+1), it++) { #ifdef HOME if (u==5) cout<<""; #endif if (u<1) continue; memset(used+1, 0, n); vector cur_ans; find_ans(nei, u, cur_ans); long long cur_val=find_val(cur_ans); if (cur_val>max_val) { max_val=cur_val; ans=cur_ans; } // cur_ans.clear(); // find_ans(nei, u, cur_ans); cur_val=find_val(cur_ans); if (cur_val>max_val) { max_val=cur_val; ans=cur_ans; } #ifdef HOME if (cur_ans.size()>4) cout<<""; #endif } return ans; } void greedy_sol::find_ans(const nei_list & nei, int u, vector & res) { used[u]=true; int max_nei=-1, sec_nei=-1; for (int i=0; imax_nei) { sec_nei=max_nei; max_nei=to; } else if (to>sec_nei) sec_nei=to; } } if (max_nei!=-1) { if (sec_nei!=-1) find_ans(nei, rand()%3?max_nei:sec_nei, res); else find_ans(nei, max_nei, res); } res.push_back(u); } long long find_val(const vector & a) { long long res=0LL; for (int i=0; i & ans) { #ifndef HOME ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); freopen("maxpath.in", "r", stdin); freopen("maxpath.out", "w", stdout); #endif cout< & a, const vector & oth) { long long cur_val=find_val(a), oth_val=find_val(oth); if (oth_val>cur_val) a=oth; }