Language: C++

#include <bits/stdc++.h>
#define maxn 100000
#define reg register
using namespace std;
inline int  read() {
    reg int s = 0, t = 0; reg char ch = getchar();
    while(ch > '9' || ch < '0') t |= ch == '-', ch = getchar();
    while(ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
    return t ? -s : s;
}
int n, a, b, p, d[maxn+5];
int fa[maxn+5], dep[maxn+5];
vector <int> G[maxn+5], Q, rev, ans;
void DFS(int u) {
	for (int v : G[u]) if (v ^ fa[u])
		fa[v] = u, dep[v] = dep[u]+1, DFS(v);
}
int main() {
	read(n);
	for (int i = 1, u, v; i < n; i++)
		read(u), read(v), d[u]++, d[v]++, 
		G[u].push_back(v), G[v].push_back(u);
	DFS(1);
	for (int i = 1; i <= n; i++)
		if (dep[i] > dep[a]) a = i;
	fa[a] = dep[a] = 0, DFS(a);
	for (int i = 1; i <= n; i++)
		if (dep[i] > dep[b]) b = i;
	for (int i = b; i; i = fa[i])
		if (i == a || i == b)
			Q.push_back(d[i]-1);
		else Q.push_back(d[i]-2);
	rev = Q, reverse(rev.begin(), rev.end());
	for (int i = 0; i < (int)Q.size(); i++)
		if (Q[i] < rev[i]) break;
		else if (Q[i] > rev[i])
			{Q = rev; break;}
	for (int c : Q) {
		p++;
		for (int i = 1; i <= c; i++)
			ans.push_back(p+i);
		ans.push_back(p), p += c;
	}
	if ((int)ans.size() < n)
		return puts("-1"), 0;
	for (int x : ans)
		printf("%d ", x);
	return puts(""), 0;
}

Compiler output:
a.cc: In function 'int read()':
a.cc:6:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    6 |     reg int s = 0, t = 0; reg char ch = getchar();
      |             ^
a.cc:6:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    6 |     reg int s = 0, t = 0; reg char ch = getchar();
      |                    ^
a.cc:6:36: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    6 |     reg int s = 0, t = 0; reg char ch = getchar();
      |                                    ^~
a.cc: In function 'int main()':
a.cc:19:13: error: no matching function for call to 'read(int&)'
   19 |         read(n);
      |         ~~~~^~~
In file included from /usr/include/x86_64-linux-gnu/bits/sigstksz.h:24,
                 from /usr/include/signal.h:328,
                 from /usr/include/c++/14/csignal:42,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:116,
                 from a.cc:1:
/usr/include/unistd.h:371:16: note: candidate: 'ssize_t read(int, void*, size_t)'
  371 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
      |                ^~~~
/usr/include/unistd.h:371:16: note:   candidate expects 3 arguments, 1 provided
a.cc:5:13: note: candidate: 'int read()'
    5 | inline int  read() {
      |             ^~~~
a.cc:5:13: note:   candidate expects 0 arguments, 1 provided
a.cc:21:21: error: no matching function for call to 'read(int&)'
   21 |                 read(u), read(v), d[u]++, d[v]++,
      |                 ~~~~^~~
/usr/include/unistd.h:371:16: note: candidate: 'ssize_t read(int, void*, size_t)'
  371 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
      |                ^~~~
/usr/include/unistd.h:371:16: note:   candidate expects 3 arguments, 1 provided
a.cc:5:13: note: candidate: 'int read()'
    5 | inline int  read() {
      |             ^~~~
a.cc:5:13: note:   candidate expects 0 arguments, 1 provided
a.cc:21:30: error: no matching function for call to 'read(int&)'
   21 |                 read(u), read(v), d[u]++, d[v]++,
      |                          ~~~~^~~
/usr/include/unistd.h:371:16: note: candidate: 'ssize_t read(int, void*, size_t)'
  371 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
      |                ^~~~
/usr/include/unistd.h:371:16: note:   candidate expects 3 arguments, 1 provided
a.cc:5:13: note: candidate: 'int read()'
    5 | inline int  read() {
      |             ^~~~
a.cc:5:13: note:   candidate expects 0 arguments, 1 provided