結果

問題 No.1098 LCAs
ユーザー KoDKoD
提出日時 2020-06-26 21:59:12
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 2,763 bytes
コンパイル時間 1,450 ms
コンパイル使用メモリ 82,564 KB
実行使用メモリ 35,880 KB
最終ジャッジ日時 2023-09-18 04:29:26
合計ジャッジ時間 6,235 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 242 ms
25,084 KB
testcase_19 AC 238 ms
25,200 KB
testcase_20 AC 240 ms
25,192 KB
testcase_21 AC 241 ms
25,320 KB
testcase_22 AC 229 ms
25,216 KB
testcase_23 AC 166 ms
26,464 KB
testcase_24 AC 167 ms
26,364 KB
testcase_25 AC 155 ms
26,368 KB
testcase_26 AC 190 ms
26,528 KB
testcase_27 AC 176 ms
26,496 KB
testcase_28 AC 271 ms
35,104 KB
testcase_29 AC 271 ms
35,880 KB
testcase_30 AC 271 ms
35,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { lhs = rhs; return true; }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { lhs = rhs; return true; }
  return false;
}

struct range {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { ++i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { }
  constexpr iterator begin() const noexcept { return l; }
  constexpr iterator end() const noexcept { return r; }
};

struct revrange {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { --i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr iterator begin() const noexcept { return r; }
  constexpr iterator end() const noexcept { return l; }
};

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

template <class T>
struct fix_point: private T {
  explicit constexpr fix_point(T &&func): T(std::forward<T>(func)) { }
  template <class... Args>
  constexpr decltype(auto) operator () (Args &&... args) const {
    return T::operator()(*this, std::forward<Args>(args)...);
  }
};

int main() {
  i32 N;
  std::cin >> N;
  std::vector<std::vector<i32>> graph(N);
  for (auto i: range(1, N)) {
    i32 u, v;
    std::cin >> u >> v;
    --u; --v;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  std::vector<std::vector<i32>> subtree(N);
  for (auto i: range(0, N)) {
    subtree[i].resize(graph[i].size());
  }
  fix_point([&](auto dfs, i32 u, i32 p) -> i32 {
    i32 res = 1;
    for (auto i: range(0, graph[u].size())) {
      i32 v = graph[u][i];
      if (v != p) {
        subtree[u][i] = dfs(v, u);
        res += subtree[u][i];
      }
    }
    return res;
  })(0, -1);
  for (auto i: range(0, N)) {
    i64 ans = 0;
    i32 sum = 0;
    for (auto x: subtree[i]) {
      sum += x;
      ans -= (i64) x * x;
    }
    ans += (i64) sum * sum;
    std::cout << ans + sum * 2 + 1 << '\n';
  }
  return 0;
}
0