Submission #1500189


Source Code Expand

#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <utility>
#include <memory>
#include <functional>
#include <deque>
#include <cctype>
#include <ctime>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll;
typedef vector<vector<long long> > vvll;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i,n) _rep2((i),0,(n))
#define _rep2(i,a,b) for(int i=(a);i<(b);++i)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define fi first
#define se second
#define mkp make_pair
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

void mainmain(){
	int n,m;
	cin>>n>>m;
	vvint vv(n);
	rep(i,m){
		int a,b;
		cin>>a>>b;
		a--,b--;
		vv[a].PB(b);
		vv[b].PB(a);
	}
	vint p(n);
	deque<int> path(1,0);
	int now = 0;
	vint used(n);
	used[0] = 1;
	while(1){
		if(p[now]==vv[now].size()) break;
		while(p[now]<vv[now].size()){
			if(used[vv[now][p[now]]]){
				p[now]++;
				continue;
			}
			now = vv[now][p[now]++];
			used[now] = 1;
			path.PB(now);
			break;
		}
	}
	now = 0;
	while(1){
		if(p[now]==vv[now].size()) break;
		while(p[now]<vv[now].size()){
			if(used[vv[now][p[now]]]){
				p[now]++;
				continue;
			}
			now = vv[now][p[now]++];
			used[now] = 1;
			path.push_front(now);
			break;
		}
	}

	cout<<path.size()<<endl;
	bool first = true;
	for(int x: path){
		if(!first) cout<<" ";
		first = false;
		cout<<x+1;
	}
	cout<<endl;
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}

Submission Info

Submission Time
Task B - Hamiltonish Path
User j_gui0121
Language C++14 (GCC 5.4.1)
Score 500
Code Size 3042 Byte
Status AC
Exec Time 44 ms
Memory 7552 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 1 ms 256 KB
sample_02.txt AC 1 ms 256 KB
sample_03.txt AC 1 ms 256 KB
subtask_1_01.txt AC 30 ms 4480 KB
subtask_1_02.txt AC 8 ms 896 KB
subtask_1_03.txt AC 28 ms 4608 KB
subtask_1_04.txt AC 31 ms 3968 KB
subtask_1_05.txt AC 31 ms 3968 KB
subtask_1_06.txt AC 32 ms 3968 KB
subtask_1_07.txt AC 32 ms 6656 KB
subtask_1_08.txt AC 35 ms 6528 KB
subtask_1_09.txt AC 44 ms 7552 KB
subtask_1_10.txt AC 8 ms 1024 KB
subtask_1_11.txt AC 10 ms 1024 KB
subtask_1_12.txt AC 1 ms 256 KB
subtask_1_13.txt AC 1 ms 256 KB