256362026-02-23 19:52:23GeneratrollUtazási irodacpp17Accepted 45/4570ms8760 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	ll n, k;
	cin >> n >> k;
	vector<vector<int>> g(n + 1);
	for (int i = 0; i < n - 1; i++) {
		int x, y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	for (int i = 1; i <= n; i++) {
		sort(g[i].begin(), g[i].end());
	}
	vector<int> v(n + 1, 0);
	vector<int> u(n + 1, 0);
	vector<int> p;
	vector<int> q;
	p.reserve(n);
	q.push_back(1);
	u[1] = -1;
	while (!q.empty()) {
		int x = q.back();
		q.pop_back();
		p.push_back(x);
		for (int y : g[x]) {
			if (u[y] == 0) {
				u[y] = x;
				q.push_back(y);
			}
		}
	}
	for (int i = n - 1; i >= 0; i--) {
		int x = p[i];
		v[x] = 1;
		for (int y : g[x]) {
			if (y != u[x]) {
				v[x] = v[x] + v[y];
			}
		}
	}
	ll a = (k - 1) / n + 1;
	ll b = (k - 1) % n + 1;
	int x = a;
	int z = -1;
	cout << x;
	while (b > 1) {
		b--;
		int f = 0;
		for (int y : g[x]) {
			if (y == z) {
				continue;
			}
			ll c;
			if (y == u[x]) {
				c = n - v[x];
			} else {
				c = v[y];
			}
			if (b <= c) {
				z = x;
				x = y;
				cout << ' ';
				cout << x;
				f = 1;
				break;
			}
			b = b - c;
		}
		if (f == 0) {
			break;
		}
	}
	cout << '\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base45/45
1Accepted0/01ms316 KiB
2Accepted0/07ms1076 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms316 KiB
5Accepted1/11ms332 KiB
6Accepted2/22ms492 KiB
7Accepted2/22ms564 KiB
8Accepted1/12ms564 KiB
9Accepted2/257ms8260 KiB
10Accepted2/261ms8244 KiB
11Accepted1/170ms8244 KiB
12Accepted2/259ms8248 KiB
13Accepted2/257ms8500 KiB
14Accepted2/261ms8264 KiB
15Accepted2/259ms8012 KiB
16Accepted2/256ms8760 KiB
17Accepted3/367ms8260 KiB
18Accepted2/259ms7988 KiB
19Accepted2/259ms8252 KiB
20Accepted2/265ms7988 KiB
21Accepted3/364ms8120 KiB
22Accepted3/357ms8168 KiB
23Accepted3/364ms7988 KiB
24Accepted1/157ms8028 KiB
25Accepted1/154ms8192 KiB