Problem:
Score: 400 points

Problem Statement

There is a cave.

The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.

It is dark in the cave, so we have decided to place a signpost in each room except Room 1. The signpost in each room will point to one of the rooms directly connected to that room with a passage.

Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1.

If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.

Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 2 \times 10^5

1 \leq A_i, B_i \leq N\ (1 \leq i \leq M)

A_i \neq B_i\ (1 \leq i \leq M)

One can travel between any two rooms by traversing passages.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If there is no way to place signposts satisfying the objective, print No.

Otherwise, print N lines. The first line should contain Yes, and the i-th line (2 \leq i \leq N) should contain the integer representing the room indicated by the signpost in Room i.

Sample Input 1

4 4
1 2
2 3
3 4
4 2

Sample Output 1

Yes
1
2
2

If we place the signposts as described in the sample output, the following happens:

Starting in Room 2, you will reach Room 1 after traversing one passage: (2) \to 1. This is the minimum number of passages possible.

Starting in Room 3, you will reach Room 1 after traversing two passages: (3) \to 2 \to 1. This is the minimum number of passages possible.

Starting in Room 4, you will reach Room 1 after traversing two passages: (4) \to 2 \to 1. This is the minimum number of passages possible.

Thus, the objective is satisfied.

Sample Input 2

6 9
3 4
6 1
2 4
5 3
4 6
1 5
6 2
4 5
5 6

Sample Output 2

Yes
6
5
5
1
1

If there are multiple solutions, any of them will be accepted.

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cassert>
#include <numeric>
#define InfL 1000000000
#define InfLL 1000000000000000000LL
#define nmax_def 220000
#define mod 1000000007
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<db> vd;

class Graph_class {
public:
	bool graph_from1 = true;
	bool graph_tree = false;
	bool graph_weight = false;
	bool graph_digraph = false;
	int N, M;
	vector<pii> edgelist;
	vl edgeweight;
	vector<vi> adjlist;
	vector<vl> adjlistweight;
	vi adjnum;
	map<pii, int> uv_to_edge;

	vi adj_to_root;
	vector<ll> DP;
	vi vlist_from_root;
	vl xdist;

	vb x_visited;
	deque<int> x_que;

	// -------

	// -------

	void read_edge();
	void read_edge_array();
	void adjini();
	void visited_ini();
	void graph_BFS(ll BFS_ini);
	void graph_DFS(ll DFS_ini);
	void graph_DFS_sbrt(ll DFS_ini);
	void Dijkstra(int x);
	bool Bellman_Ford(int x);
	ll diameter();
	void ReRooting(ll root);
	Graph_class Kruskal();
	int dim_num(int dim);
	void solve();

	bool xydist_def = false;
};

// ----- UnionFind -----

class UnionFind {
public:
	vi par;
	vi adjnum;
	int N;

	void UnionFind_ini(int Narg);
	int root(int x);
	void unite(int x, int y);
	bool same(int x, int y);
	int adjnum_func(int x);
};

void UnionFind::UnionFind_ini(int Narg) {
	N = Narg;
	adjnum.resize(N);
	par.resize(N);
	rep(i, N) {
		adjnum[i] = 1;
		par[i] = i;
	}
}

int UnionFind::root(int x) {
	if (par[x] == x)
		return x;
	par[x] = root(par[x]);
	return par[x];
}

void UnionFind::unite(int x, int y) {
	int rx = root(x);
	int ry = root(y);
	if (rx == ry)
		return;
	par[rx] = ry;
	adjnum[ry] += adjnum[rx];
	return;
}

bool UnionFind::same(int x, int y) {
	int rx = root(x);
	int ry = root(y);
	return rx == ry;
}

int UnionFind::adjnum_func(int x) {
	int rx = root(x);
	return adjnum[rx];
}

// ----- graph -----

void Graph_class::read_edge() {
	cin >> N;
	if (graph_tree)
		M = N - 1;// graph
	else
		cin >> M;
	edgelist.resize(M);
	edgeweight.resize(M, 1);
	rep(i, M) {
		if (graph_weight)
			cin >> edgelist[i].first >> edgelist[i].second >> edgeweight[i];
		else
			cin >> edgelist[i].first >> edgelist[i].second;
		if (graph_from1) {
			edgelist[i].first--;
			edgelist[i].second--;
		}
		//uv_to_edge[edgelist[i]] = i;
	}
	return;
}

void Graph_class::read_edge_array() {
	cin >> N;
	M = 0;
	rep(i, N) {
		string S;
		cin >> S;
		rep(j, N) {
			if (S[j] == '1') {
				if (graph_digraph || i < j) {
					pii edgetmp = make_pair(i, j);
					edgelist.push_back(edgetmp);
					M++;
				}
			}
		}
	}
	if (!graph_digraph)
		M /= 2;
	return;
}

void Graph_class::adjini() {
	adjnum.resize(N);
	xdist.resize(N, -1);
	adjlist.resize(N);
	adjlistweight.resize(N);
	adj_to_root.resize(N, -1);
	DP.resize(N, 0);
	rep(i, N)
		adjnum[i] = 0;
	rep(i, M) {
		int x = edgelist[i].first;
		int y = edgelist[i].second;
		ll xyweight = edgeweight[i];
		adjlist[x].push_back(y);
		adjlistweight[x].push_back(xyweight);
		adjnum[x]++;
		if (!graph_digraph) {
			adjlist[y].push_back(x);
			adjlistweight[y].push_back(xyweight);
			adjnum[y]++;
		}
	}
	x_visited.resize(N);
	visited_ini();

	return;
}

void Graph_class::visited_ini() {
	rep(i, N) {
		x_visited[i] = false;
		adj_to_root[i] = -1;
	}
	x_que.clear();
}

void Graph_class::graph_BFS(ll BFS_ini) {
	visited_ini();
	x_que.push_back(BFS_ini);
	x_visited[BFS_ini] = true;

	// option
	xdist[BFS_ini] = 0;
	vlist_from_root.clear();
	vlist_from_root.push_back(BFS_ini);
	//cout << BFS_ini << endl;

	while (!x_que.empty()) {
		int x = x_que.front();
		x_que.pop_front();
		rep(i, adjnum[x]) {
			int x_new = adjlist[x][i];
			ll dist_new = adjlistweight[x][i];
			if (!x_visited[x_new]) {
				x_visited[x_new] = true;
				x_que.push_back(x_new);

				// option
				adj_to_root[x_new] = x;
				vlist_from_root.push_back(x_new);
				xdist[x_new] = xdist[x] + dist_new;
				//cout << x_new << endl;
			}
		}
	}
	return;
}

void Graph_class::graph_DFS(ll DFS_ini) {
	visited_ini();
	x_que.push_back(DFS_ini);
	x_visited[DFS_ini] = true;

	while (!x_que.empty()) {
		int x = x_que.back();
		x_que.pop_back();

		// option
		//cout << x << endl;

		rep(i, adjnum[x]) {
			int x_new = adjlist[x][i];
			if (!x_visited[x_new]) {
				x_visited[x_new] = true;
				x_que.push_back(x_new);
			}
		}
	}
	return;
}

void Graph_class::graph_DFS_sbrt(ll DFS_ini) {
	x_visited[DFS_ini] = true;

	// in


	rep(i, adjnum[DFS_ini]) {
		int x_new = adjlist[DFS_ini][i];
		if (!x_visited[x_new]) {
			x_visited[x_new] = true;
			graph_DFS_sbrt(x_new);
		}
	}

	// out


	return;
}

void Graph_class::Dijkstra(int x) {
	priority_queue<tuple<ll, int>, vector<tuple<ll, int> >, greater<tuple<ll, int> > > xdist_que;
	rep(i, N)
		xdist[i] = InfLL;
	xdist[x] = 0;
	xdist_que.push(make_tuple(0, x));
	while (!xdist_que.empty()) {
		tuple<ll, int> xdist_que_tmp = xdist_que.top();
		xdist_que.pop();
		ll dist_tmp = get<0>(xdist_que_tmp);
		int x_tmp = get<1>(xdist_que_tmp);
		if (xdist[x_tmp] < dist_tmp)
			continue;
		rep(i, adjnum[x_tmp]) {
			int x_new = adjlist[x_tmp][i];
			ll dist_new = dist_tmp + adjlistweight[x_tmp][i];
			if (xdist[x_new] > dist_new) {
				xdist[x_new] = dist_new;
				xdist_que.push(make_tuple(dist_new, x_new));
			}
		}
	}
	return;
}

bool Graph_class::Bellman_Ford(int x) {
	rep(i, N)
		xdist[i] = InfLL;
	xdist[x] = 0;
	rep(xtmp, N) {
		rep(etmp, M) {
			ll weighttmp = edgeweight[etmp];
			int x_from = edgelist[etmp].first;
			int x_to = edgelist[etmp].second;
			if (xdist[x_to] > xdist[x_from] + weighttmp) {
				xdist[x_to] = xdist[x_from] + weighttmp;
				if (xtmp == N - 1)
					return true;
			}
		}
	}
	return false;
}

ll Graph_class::diameter() {
	ll anstmp = 0;
	if (graph_tree) {
		ll anstmp_i = 0;
		pii longest;
		graph_BFS(0);
		rep(i, N) {
			if (xdist[i] > anstmp) {
				anstmp = xdist[i];
				anstmp_i = i;
			}
		}
		longest.first = anstmp_i;
		anstmp = 0, anstmp_i = 0;
		graph_BFS(longest.first);
		rep(i, N) {
			if (xdist[i] > anstmp) {
				anstmp = xdist[i];
				anstmp_i = i;
			}
		}
		longest.second = anstmp_i;
	}
	else {
		rep(i, N) {
			Dijkstra(i);
			rep(j, N) {
				anstmp = max(anstmp, xdist[j]);
			}
		}
	}
	return anstmp;
}


void Graph_class::ReRooting(ll root) {
	rep(i, N) {
		DP[i] = 1;
	}
	graph_BFS(root);
	rrep(i, N) { // 根から遠い点から見る
		ll x = vlist_from_root[i];
		rep(j, adjnum[x]) {
			ll x_new = adjlist[x][j];
			if (x_new == adj_to_root[x]) // 根側
				continue;
			DP[x] += DP[x_new];
		}
	}
	return;
}

Graph_class Graph_class::Kruskal() {
	assert(!graph_digraph);
	Graph_class tree;
	vector<pair<ll, pii> > edgeweight_sort(M);
	rep(i, M) {
		edgeweight_sort[i].first = edgeweight[i];
		edgeweight_sort[i].second = edgelist[i];
	}
	sort(edgeweight_sort.begin(), edgeweight_sort.end(), less<pair<ll, pii> >());
	UnionFind tree_uf;
	tree_uf.UnionFind_ini(N);
	tree.N = N;
	tree.M = 0;
	rep(i, M) {
		ll edgeweighttmp = edgeweight_sort[i].first;
		pii edgetmp = edgeweight_sort[i].second;
		if (!tree_uf.same(edgetmp.first, edgetmp.second)) {
			tree_uf.unite(edgetmp.first, edgetmp.second);
			tree.edgelist.push_back(edgetmp);
			tree.edgeweight.push_back(edgeweighttmp);
			tree.M++;
		}
	}
	ll ans = 0;
	rep(i, tree.M) {
		ans += tree.edgeweight[i];
		cout << (tree.edgelist[i].first + 1) << " " << (tree.edgelist[i].second + 1) << " " << tree.edgeweight[i] << endl;
	}
	cout << ans << endl;

	tree.graph_from1 = true;
	tree.graph_tree = true;
	tree.graph_weight = true;
	tree.graph_digraph = false;

	tree.adjini();
	return tree;
}


int Graph_class::dim_num(int dim) {
	int anstmp = 0;
	rep(i, N) {
		if (adjnum[i] == dim)
			anstmp++;
	}
	return anstmp;
}

void Graph_class::solve() {
	cout << "Yes" << endl;
	graph_BFS(0);
	rep(i, N) {
		if (i == 0)
			continue;
		cout << adj_to_root[i] + 1 << endl;
	}

	//Graph_class tree = Kruskal();
	return;
}

int main(void) {
	Graph_class graph;
	graph.read_edge();
	graph.adjini();
	graph.solve();
	return 0;
}