CompetitiveProgrammingCpp

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub

:heavy_check_mark: Test/Graph/Tree/TreeDP.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/763"

#include <iostream>

#include <queue>

#include <vector>


using ll = long long;
using std::cin;
using std::cout;
constexpr char endl = '\n';

#include "./../../../Library/Graph/Graph.hpp"

#include "./../../../Library/Graph/Tree/TreeDP.hpp"


signed main() {
  ll n;
  cin >> n;
  mtd::Graph<int, bool> tree(n);
  for (int f = 0; f < n - 1; ++f) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    tree.addEdge(u, v);
  }

  std::vector<int> dp1(n);
  std::vector<int> dp2(n, 1);
  mtd::treeDP(tree, 0, [&](int f, int t, int _) {
    dp1[t] += std::max(dp1[f], dp2[f]);
    dp2[t] += std::max(dp1[f], dp2[f] - 1);
  });

  auto ans = std::max(dp1[0], dp2[0]);
  cout << ans << endl;
}
#line 1 "Test/Graph/Tree/TreeDP.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/763"

#include <iostream>

#include <queue>

#include <vector>


using ll = long long;
using std::cin;
using std::cout;
constexpr char endl = '\n';

#line 2 "Library/Graph/Graph.hpp"
#include <deque>

#line 4 "Library/Graph/Graph.hpp"
#include <ranges>

#include <tuple>

#line 7 "Library/Graph/Graph.hpp"

namespace mtd {
  template <class Node = long long, class Cost = long long>
  class Graph {
    using Edge = std::pair<Node, Cost>;
    using Edges = std::vector<Edge>;

    const int m_n;
    std::vector<Edges> m_graph;

  public:
    Graph(int n) : m_n(n), m_graph(n) {}
    Graph(const std::vector<Edges>& edges)
        : m_n(edges.size()), m_graph(edges) {}
    Graph(int n, const std::vector<std::tuple<Node, Node>>& edges,
          bool is_arc = false, bool is_index1 = true)
        : Graph<Node, Cost>(n) {
      for (auto [u, v] : edges) {
        u -= is_index1;
        v -= is_index1;
        if (is_arc) {
          addArc(u, v);
        } else {
          addEdge(u, v);
        }
      }
    }
    Graph(int n, const std::vector<std::tuple<Node, Node, Cost>>& edges,
          bool is_arc = false, bool is_index1 = true)
        : Graph<Node, Cost>(n) {
      for (auto [u, v, c] : edges) {
        u -= is_index1;
        v -= is_index1;
        if (is_arc) {
          addArc(u, v, c);
        } else {
          addEdge(u, v, c);
        }
      }
    }

    auto addEdge(const Node& f, const Node& t, const Cost& c = 1) {
      addArc(f, t, c);
      addArc(t, f, c);
    }
    auto addArc(const Node& f, const Node& t, const Cost& c = 1) {
      m_graph[f].emplace_back(t, c);
    }
    auto getEdges(const Node& from) const {
      class EdgesRange {
        const typename Edges::const_iterator b, e;

      public:
        EdgesRange(const Edges& edges) : b(edges.begin()), e(edges.end()) {}
        auto begin() const { return b; }
        auto end() const { return e; }
      };
      return EdgesRange(m_graph[from]);
    }
    auto getEdges() const {
      std::deque<std::tuple<Node, Node, Cost>> edges;
      for (Node from : std::views::iota(0, m_n)) {
        for (const auto& [to, c] : getEdges(from)) {
          edges.emplace_back(from, to, c);
        }
      }
      return edges;
    }
    auto getEdgesExcludeCost() const {
      std::deque<std::pair<Node, Node>> edges;
      for (Node from : std::views::iota(0, m_n)) {
        for (const auto& [to, _] : getEdges(from)) {
          edges.emplace_back(from, to);
        }
      }
      return edges;
    }
    auto reverse() const {
      auto rev = Graph<Node, Cost>(m_n);
      for (const auto& [from, to, c] : getEdges()) { rev.addArc(to, from, c); }
      return rev;
    }
    auto size() const { return m_n; };
    auto debug(bool directed = false) const {
      for (const auto& [f, t, c] : getEdges()) {
        if (f < t || directed) {
          std::cout << f << " -> " << t << ": " << c << std::endl;
        }
      }
    }
  };
}  // namespace mtd

#line 2 "Library/Graph/Tree/TreeDP.hpp"

#include <concepts>

#line 6 "Library/Graph/Tree/TreeDP.hpp"

#line 8 "Library/Graph/Tree/TreeDP.hpp"

namespace mtd {
  template <class Node, class Cost, class Lambda,
            std::convertible_to<Node> _Node>
  auto treeDP(const Graph<Node, Cost>& tree, _Node root, const Lambda& lambda) {
    auto n = tree.size();
    std::vector<Node> in(n);
    for (const auto& [f, t] : tree.getEdgesExcludeCost())
      if (f < t) {
        ++in[f];
        ++in[t];
      }
    std::queue<Node> q;
    std::vector<bool> used(n);
    for (Node i = 0; i < n; ++i)
      if (i != root && in[i] == 1) { q.emplace(i); }
    while (!q.empty()) {
      auto from = q.front();
      q.pop();
      used[from] = true;

      for (const auto& [to, cost] : tree.getEdges(from)) {
        if (used[to]) { continue; }
        lambda(from, to, cost);
        --in[to];
        if (to != root && in[to] == 1) { q.emplace(to); }
      }
    }
  }
}  // namespace mtd

#line 14 "Test/Graph/Tree/TreeDP.test.cpp"

signed main() {
  ll n;
  cin >> n;
  mtd::Graph<int, bool> tree(n);
  for (int f = 0; f < n - 1; ++f) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    tree.addEdge(u, v);
  }

  std::vector<int> dp1(n);
  std::vector<int> dp2(n, 1);
  mtd::treeDP(tree, 0, [&](int f, int t, int _) {
    dp1[t] += std::max(dp1[f], dp2[f]);
    dp2[t] += std::max(dp1[f], dp2[f] - 1);
  });

  auto ans = std::max(dp1[0], dp2[0]);
  cout << ans << endl;
}
Back to top page