| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1097029 | vlad1_1 | Mousetrap (CEOI17_mousetrap) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
// https://oj.uz/problem/view/CEOI17_mousetrap
using namespace std;
void dfs(int node, int parent, vector<vector<int>>& graph, vector<int>& parents, vector<vector<int>>& depths, int depth = 0) {
if (depths.size() <= depth) {
depths.resize(4 * (depth + 1));
}
depths[depth].push_back(node);
for (int child : graph[node]) {
if (child == parent) continue;
parents[child] = node;
dfs(child, node, graph, parents, depths, depth + 1);
}
}
auto max2(ranges::input_range auto&& range) {
using T = ranges::range_value_t<decltype(range)>;
auto it = ranges::begin(range);
auto sentinel = ranges::end(range);
if (it == sentinel) {
return 0;
}
auto max1 = *it++;
if (it == sentinel) {
return 0;
}
auto max2 = *it++;
if (max1 < max2) {
swap(max1, max2);
}
for (; it != sentinel; it++) {
if (*it > max1) {
max2 = max1;
max1 = *it;
} else if (*it > max2) {
max2 = *it;
}
}
return max2;
}
int main() {
int n, t, m;
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> t >> m;
vector<vector<int>> graph(n+1);
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
assert(find(graph[t].begin(), graph[t].end(), m) != graph[t].end());
graph[t] = {m};
vector<int> parents(n+1);
vector<vector<int>> depths;
parents[t] = 0;
dfs(t, 0, graph, parents, depths);
vector<int> dp(n+1);
for (auto& row : depths | views::reverse) {
for (int node : row) {
dp[node] = graph[node].size() + max2(graph[node]
| views::filter([&parents, &dp, &node](int child) { return child != parents[node]; })
| views::transform([&dp](int child) { return dp[child]; }));
}
}
cout << dp[m] << '\n';
}
