提出 #34360606
ソースコード 拡げる
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<vector<int>> graph(N); for (int i = 0; i < N - 1; ++i) { int a, b; cin >> a >> b; a -= 1; b -= 1; graph[a].push_back(b); graph[b].push_back(a); } int Q; cin >> Q; vector<vector<pair<int, int>>> query(N); for (int i = 0; i < Q; ++i) { int u, k; cin >> u >> k; u -= 1; query[u].emplace_back(i, k); } auto get_farthest = [&](int src) { vector<int> dist(N, N); queue<int> que; auto push = [&](int u, int d) { if (dist[u] > d) { dist[u] = d; que.push(u); } }; push(src, 0); while (!que.empty()) { int u = que.front(); que.pop(); for (int v : graph[u]) { push(v, dist[u] + 1); } } return max_element(dist.begin(), dist.end()) - dist.begin(); }; int L = get_farthest(0); int R = get_farthest(L); vector<int> ans(Q, -1); for (int root : {L, R}) { vector<int> path; auto dfs = [&](auto&& dfs, int u, int p) -> void { for (const auto& [q, k] : query[u]) { if (k <= (int)path.size()) { ans[q] = path[(int)path.size() - k] + 1; } } path.push_back(u); for (int v : graph[u]) { if (v != p) { dfs(dfs, v, u); } } path.pop_back(); }; dfs(dfs, root, -1); } for (int x : ans) { cout << x << '\n'; } return 0; }
提出情報
提出日時 | |
---|---|
問題 | F - Exactly K Steps |
ユーザ | KoD |
言語 | C++ (GCC 9.2.1) |
得点 | 500 |
コード長 | 1804 Byte |
結果 | AC |
実行時間 | 321 ms |
メモリ | 38016 KiB |
ジャッジ結果
セット名 | Sample | All | ||||
---|---|---|---|---|---|---|
得点 / 配点 | 0 / 0 | 500 / 500 | ||||
結果 |
|
|
セット名 | テストケース |
---|---|
Sample | example_00.txt, example_01.txt |
All | example_00.txt, example_01.txt, test_00.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt |
ケース名 | 結果 | 実行時間 | メモリ |
---|---|---|---|
example_00.txt | AC | 8 ms | 3564 KiB |
example_01.txt | AC | 2 ms | 3428 KiB |
test_00.txt | AC | 247 ms | 38016 KiB |
test_01.txt | AC | 7 ms | 3428 KiB |
test_02.txt | AC | 311 ms | 29524 KiB |
test_03.txt | AC | 282 ms | 25652 KiB |
test_04.txt | AC | 266 ms | 25656 KiB |
test_05.txt | AC | 321 ms | 35120 KiB |
test_06.txt | AC | 286 ms | 23984 KiB |
test_07.txt | AC | 260 ms | 25608 KiB |
test_08.txt | AC | 83 ms | 7144 KiB |
test_09.txt | AC | 85 ms | 7008 KiB |
test_10.txt | AC | 80 ms | 7008 KiB |
test_11.txt | AC | 84 ms | 13436 KiB |
test_12.txt | AC | 83 ms | 11492 KiB |
test_13.txt | AC | 76 ms | 12428 KiB |
test_14.txt | AC | 314 ms | 31388 KiB |
test_15.txt | AC | 302 ms | 23648 KiB |
test_16.txt | AC | 263 ms | 25148 KiB |
test_17.txt | AC | 256 ms | 25368 KiB |
test_18.txt | AC | 225 ms | 19384 KiB |
test_19.txt | AC | 215 ms | 19880 KiB |