Submission #1825704


Source Code Expand

// see https://gist.github.com/LumaKernel/ff55d49ee1af69b7388f15b707e75c15
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <complex>
#include <random>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef vector<P> VP;

#define omajinai ios::sync_with_stdio(false);cin.tie(0)

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define RREP(i,n) RFOR(i,0,n)

#define LFOR(i,a,b) for(ll i=(a);i<(b);++i)
#define RLFOR(i,b,a) for(ll i=(b)-1;i>=(a);--i)

#define ALL(a) (a).begin(),(a).end()
#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())
#define MP make_pair
#define PB push_back
#define EACH(i,c) REP(i,(int)(c).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

#define dump(x) cerr << "[L " << __LINE__ << "] " << #x << " = " << (x) << "\n";
#define dump2(x,y) cerr << "[L " << __LINE__ << "] " << #x << " = " << (x)\
			<< " , " << #y << " = " << (y) << "\n";

const int INF = 1e9;
const double EPS = 1e-10;

VI G[100000];
int main() {
	int n,m;
	cin >> n >> m;
	REP(i, m){
		int a,b;
		cin >> a >> b;
		a--;b--;
		G[a].PB(b);
		G[b].PB(a);
	}

	VI ans;
	set<int> st;
	ans.PB(0);
	st.insert(0);

	while(1){
		bool flag = 1;
		for(auto v : G[ans[0]]){
			if(!EXIST(st, v)){
				ans.insert(ans.begin(), v);
				st.insert(v);
				flag = 0;
				break;
			}
		}
		if(!flag) continue;
		flag = 1;
		for(auto v : G[ans[ans.size()-1]]){
			if(!EXIST(st, v)){
				ans.push_back(v);
				st.insert(v);
				flag = 0;
				break;
			}
		}
		if(!flag) continue;
		break;
	}
	cout << ans.size() << endl;
	for(auto v: ans) cout << v+1 << (v==ans[ans.size()-1] ? "\n" : " ");
}

Submission Info

Submission Time
Task B - Hamiltonish Path
User luma
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1994 Byte
Status AC
Exec Time 752 ms
Memory 11384 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 19
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 2 ms 2560 KB
sample_02.txt AC 2 ms 2560 KB
sample_03.txt AC 2 ms 2560 KB
subtask_1_01.txt AC 68 ms 4736 KB
subtask_1_02.txt AC 18 ms 3072 KB
subtask_1_03.txt AC 61 ms 4864 KB
subtask_1_04.txt AC 71 ms 4736 KB
subtask_1_05.txt AC 71 ms 4736 KB
subtask_1_06.txt AC 73 ms 4736 KB
subtask_1_07.txt AC 75 ms 5888 KB
subtask_1_08.txt AC 75 ms 5760 KB
subtask_1_09.txt AC 752 ms 11384 KB
subtask_1_10.txt AC 23 ms 3328 KB
subtask_1_11.txt AC 27 ms 3328 KB
subtask_1_12.txt AC 2 ms 2560 KB
subtask_1_13.txt AC 2 ms 2560 KB