67422023-12-18 15:34:19111Utazási irodacpp17Accepted 45/4559ms25056 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double

#define pii pair<int, int>

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef CB
	freopen("be2.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
	int N, K;
	cin >> N >> K;
	vector<vector<int>> g(N + 1);
	for (int i = 0; i < N - 1; i++) {
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	K--;
	int s = 1 + K / N;
	K %= N;
	vector<int> d(N + 1, 1);
	auto dfs1 = [&](auto dfs1, int i, int j) -> void {
		for (int k : g[j]) {
			if (k != i) {
				dfs1(dfs1, j, k);
				d[j] += d[k];
			}
		}
	};
	dfs1(dfs1, s, s);
	vector<int> ans;
	auto dfs2 = [&](auto dfs2, int i, int j) -> void {
		ans.push_back(j);
		sort(g[j].begin(), g[j].end());
		for (int k : g[j]) {
			if (k != i) {
				if (K <= d[k]) {
					if (K > 0) {
						K--;
						dfs2(dfs2, j, k);
					}
				}
				else {
					K -= d[k];
				}
			}
		}
	};
	dfs2(dfs2, s, s);
	for (int i : ans) {
		cout << i << ' ';
	}
	cout << '\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base45/45
1Accepted0/03ms1828 KiB
2Accepted0/08ms3480 KiB
3Accepted2/23ms2240 KiB
4Accepted2/23ms2460 KiB
5Accepted1/13ms2808 KiB
6Accepted2/24ms3260 KiB
7Accepted2/23ms3484 KiB
8Accepted1/14ms3948 KiB
9Accepted2/256ms23468 KiB
10Accepted2/257ms24040 KiB
11Accepted1/159ms25056 KiB
12Accepted2/250ms17156 KiB
13Accepted2/243ms18196 KiB
14Accepted2/250ms17352 KiB
15Accepted2/250ms17348 KiB
16Accepted2/246ms18272 KiB
17Accepted3/350ms17364 KiB
18Accepted2/250ms17664 KiB
19Accepted2/246ms17632 KiB
20Accepted2/248ms18704 KiB
21Accepted3/348ms17680 KiB
22Accepted3/348ms18020 KiB
23Accepted3/352ms18496 KiB
24Accepted1/148ms18692 KiB
25Accepted1/152ms19832 KiB