답안 #1097029

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1097029 2024-10-05T20:30:46 Z vlad1_1 Mousetrap (CEOI17_mousetrap) C++17
컴파일 오류
0 ms 0 KB
#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';
}

Compilation message

mousetrap.cpp: In function 'void dfs(int, int, std::vector<std::vector<int> >&, std::vector<int>&, std::vector<std::vector<int> >&, int)':
mousetrap.cpp:8:23: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
    8 |     if (depths.size() <= depth) {
      |         ~~~~~~~~~~~~~~^~~~~~~~
mousetrap.cpp: At global scope:
mousetrap.cpp:20:11: error: 'ranges' has not been declared
   20 | auto max2(ranges::input_range auto&& range) {
      |           ^~~~~~
mousetrap.cpp:20:45: error: expected ',' or ';' before '{' token
   20 | auto max2(ranges::input_range auto&& range) {
      |                                             ^
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:72:31: error: 'views' has not been declared
   72 |     for (auto& row : depths | views::reverse) {
      |                               ^~~~~
mousetrap.cpp:75:19: error: 'views' has not been declared
   75 |                 | views::filter([&parents, &dp, &node](int child) { return child != parents[node]; })
      |                   ^~~~~
mousetrap.cpp:76:19: error: 'views' has not been declared
   76 |                 | views::transform([&dp](int child) { return dp[child]; }));
      |                   ^~~~~