結果
問題 |
No.1718 Random Squirrel
|
ユーザー |
![]() |
提出日時 | 2021-10-22 21:56:51 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,785 bytes |
コンパイル時間 | 1,126 ms |
コンパイル使用メモリ | 89,400 KB |
実行使用メモリ | 20,480 KB |
最終ジャッジ日時 | 2024-09-23 05:23:29 |
合計ジャッジ時間 | 6,578 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 WA * 11 |
ソースコード
#include <queue> #include <vector> #include <iostream> #include <algorithm> #include <functional> using namespace std; int main() { int N, K; cin >> N >> K; vector<vector<int> > G(N); for (int i = 0; i < N - 1; ++i) { int a, b; cin >> a >> b; --a, --b; G[a].push_back(b); G[b].push_back(a); } vector<int> D(K); for (int i = 0; i < K; ++i) { cin >> D[i]; --D[i]; } vector<bool> donguri(N, false); for (int i = 0; i < K; ++i) { donguri[D[i]] = true; } vector<int> subsize(N); vector<int> overway(N, -1), depth(N); function<void(int, int, int)> dfs = [&](int pos, int pre, int id) { if (donguri[pos]) { subsize[pos] = 1; id = pos; } overway[pos] = id; for (int i : G[pos]) { if (i == pre) continue; depth[i] = depth[pos] + 1; dfs(i, pos, id); subsize[pos] += subsize[i]; } }; dfs(D[0], -1, D[0]); function<vector<int>(int)> shortest_path = [&](int pos) { queue<int> que; que.push(pos); vector<int> resdist(N, -1); resdist[pos] = 0; while (!que.empty()) { int u = que.front(); que.pop(); for (int i : G[u]) { if (resdist[i] == -1 && subsize[i] >= 1) { resdist[i] = resdist[u] + 1; que.push(i); } } } return resdist; }; vector<int> v0 = shortest_path(D[0]); int p1 = max_element(v0.begin(), v0.end()) - v0.begin(); vector<int> v1 = shortest_path(p1); int p2 = max_element(v1.begin(), v1.end()) - v1.begin(); vector<int> v2 = shortest_path(p2); int base_cost = -2; for (int i = 0; i < N; ++i) { if (subsize[i] >= 1) { base_cost += 2; } } for (int i = 0; i < N; ++i) { if (subsize[i] >= 1) { cout << base_cost - max(v1[i], v2[i]) << endl; } else { cout << base_cost + (depth[i] - depth[overway[i]]) - max(v1[overway[i]], v2[overway[i]]) << endl; } } return 0; }