Submission #1501143
Source Code Expand
#include <bits/stdc++.h> #define pb push_back using namespace std; const int maxn = 1e5 + 7; vector<int> G[maxn]; int pi[maxn]; int dep[maxn]; bool vis[maxn]; int lf; void dfs(int x,int h) { int ch = 0; dep[x] = h; for (int j = 0; j < G[x].size(); j++) { int z = G[x][j]; if (pi[z] > -2) continue; pi[z] = x; ch++; dfs(z,h+1); } if (ch == 0) lf = x; } vector<int> p; int main() { ios::sync_with_stdio(false); cin.tie(0); int n,m; cin>>n>>m; for (int i = 1; i <= n; i++) { pi[i] = -2; dep[i] = 0; vis[i] = false; } for (int i = 1; i <= m; i++) { int x,y; cin>>x>>y; G[x].pb(y); G[y].pb(x); } pi[1] = -1; dfs(1,0); int cur = lf; int maxh = 0; p.clear(); while (true) { while (dep[cur] >= maxh) { //cout<<cur<<" : "<<pi[cur]<<endl; vis[cur] = true; p.pb(cur); if (pi[cur] < 0 || vis[pi[cur]]) break; cur = pi[cur]; } maxh = dep[cur] + 1; bool ok = false; for (int j = 0; j < G[cur].size(); j++) { if (!vis[ G[cur][j] ]) { cur = G[cur][j]; ok = true; break; } } if (!ok) break; } cout<<p.size()<<endl; for (int i = 0; i < p.size(); i++) { cout<<p[i]<<" "; } cout<<endl; }
Submission Info
Submission Time | |
---|---|
Task | B - Hamiltonish Path |
User | mbrc |
Language | C++14 (GCC 5.4.1) |
Score | 500 |
Code Size | 1228 Byte |
Status | AC |
Exec Time | 56 ms |
Memory | 13432 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 500 / 500 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | sample_01.txt, sample_02.txt, sample_03.txt |
All | sample_01.txt, sample_02.txt, sample_03.txt, sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_01.txt, subtask_1_02.txt, subtask_1_03.txt, subtask_1_04.txt, subtask_1_05.txt, subtask_1_06.txt, subtask_1_07.txt, subtask_1_08.txt, subtask_1_09.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
sample_01.txt | AC | 10 ms | 3196 KB |
sample_02.txt | AC | 2 ms | 2560 KB |
sample_03.txt | AC | 2 ms | 2560 KB |
subtask_1_01.txt | AC | 35 ms | 7168 KB |
subtask_1_02.txt | AC | 9 ms | 3456 KB |
subtask_1_03.txt | AC | 36 ms | 8192 KB |
subtask_1_04.txt | AC | 38 ms | 7424 KB |
subtask_1_05.txt | AC | 39 ms | 7552 KB |
subtask_1_06.txt | AC | 39 ms | 7680 KB |
subtask_1_07.txt | AC | 38 ms | 6656 KB |
subtask_1_08.txt | AC | 38 ms | 6656 KB |
subtask_1_09.txt | AC | 56 ms | 13432 KB |
subtask_1_10.txt | AC | 9 ms | 3328 KB |
subtask_1_11.txt | AC | 11 ms | 3456 KB |
subtask_1_12.txt | AC | 2 ms | 2560 KB |
subtask_1_13.txt | AC | 2 ms | 2560 KB |