結果

問題 No.763 Noelちゃんと木遊び
ユーザー hashiryohashiryo
提出日時 2020-05-17 23:41:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 2,386 bytes
コンパイル時間 1,600 ms
コンパイル使用メモリ 177,236 KB
実行使用メモリ 16,536 KB
最終ジャッジ日時 2023-07-24 17:48:32
合計ジャッジ時間 4,137 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
16,536 KB
testcase_01 AC 18 ms
6,388 KB
testcase_02 AC 58 ms
11,200 KB
testcase_03 AC 36 ms
8,564 KB
testcase_04 AC 22 ms
6,920 KB
testcase_05 AC 25 ms
8,364 KB
testcase_06 AC 53 ms
12,976 KB
testcase_07 AC 47 ms
12,432 KB
testcase_08 AC 28 ms
8,824 KB
testcase_09 AC 19 ms
6,944 KB
testcase_10 AC 8 ms
4,648 KB
testcase_11 AC 55 ms
13,044 KB
testcase_12 AC 58 ms
12,072 KB
testcase_13 AC 57 ms
11,812 KB
testcase_14 AC 39 ms
10,676 KB
testcase_15 AC 26 ms
8,640 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 27 ms
8,504 KB
testcase_18 AC 44 ms
12,852 KB
testcase_19 AC 48 ms
12,008 KB
testcase_20 AC 45 ms
12,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                           \
  for (long long hoge = 0; (hoge) < (n); ++(hoge)) \
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl

template <typename T, typename E>
struct Tree_DP {
  struct Edge {
    int to;
    E data;
    T dp, ndp;
  };
  using F = function<T(T, T)>;
  using G = function<T(T, E)>;

 private:
  vector<vector<Edge>> graph;
  vector<T> subdp, dp;
  const T init;
  const F f;
  const G g;

 private:
  void dfs_sub(int idx, int par) {
    for (auto &e : graph[idx]) {
      if (e.to == par) continue;
      dfs_sub(e.to, idx);
      subdp[idx] = f(subdp[idx], g(subdp[e.to], e.data));
    }
  }

  void dfs_all(int idx, int par, const T &top) {
    T buff{init};
    for (int i = 0; i < (int)graph[idx].size(); i++) {
      auto &e = graph[idx][i];
      e.ndp = buff;
      e.dp = g(par == e.to ? top : subdp[e.to], e.data);
      buff = f(buff, e.dp);
    }
    dp[idx] = buff;
    buff = init;
    for (int i = (int)graph[idx].size() - 1; i >= 0; i--) {
      auto &e = graph[idx][i];
      if (e.to != par) dfs_all(e.to, idx, f(e.ndp, buff));
      e.ndp = f(e.ndp, buff);
      buff = f(buff, e.dp);
    }
  }

 public:
  Tree_DP(int V, const F f, const G g, T init)
      : graph(V), f(f), g(g), init(init), subdp(V, init), dp(V, init) {}

  void add_edge(int u, int v, E d, E e) {
    graph[u].emplace_back((Edge){v, d, init, init});
    graph[v].emplace_back((Edge){u, e, init, init});
  }
  void add_edge(int u, int v, E d = E()) { add_edge(u, v, d, d); }

  T build(int root) {
    dfs_sub(root, -1);
    return subdp[root];
  }

  vector<T> build_rerooting() {
    dfs_sub(0, -1);
    dfs_all(0, -1, init);
    return dp;
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N;
  cin >> N;
  using Pii = pair<int, int>;
  auto f = [](const Pii &vdp, const Pii &chdp) {
    Pii ret = vdp;
    ret.first += max(chdp.first - 1, chdp.second);
    ret.second += max(chdp.first, chdp.second);
    return ret;
  };
  auto g = [](const Pii &dp, int dat) { return dp; };
  Tree_DP<Pii, int> graph(N, f, g, {1, 0});
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    graph.add_edge(u, v);
  }
  auto ans = graph.build(0);
  cout << max(ans.first, ans.second) << endl;
  return 0;
}
0