結果
問題 | No.1817 Reversed Edges |
ユーザー | Cyanmond |
提出日時 | 2022-01-21 21:55:16 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 258 ms / 2,000 ms |
コード長 | 2,871 bytes |
コンパイル時間 | 2,200 ms |
コンパイル使用メモリ | 199,200 KB |
最終ジャッジ日時 | 2025-01-27 13:42:14 |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 23 |
ソースコード
#line 1 "paper.cpp" #include <bits/stdc++.h> #line 3 "library2/utility/int_alias.hpp" using i8 = std::int8_t; using u8 = std::uint8_t; using i16 = std::int16_t; using i32 = std::int32_t; using i64 = std::int64_t; using u16 = std::uint16_t; using u32 = std::uint32_t; using u64 = std::uint64_t; #line 2 "library2/utility/rec_lambda.hpp" template <class F> class RecursiveLambda { F f; public: explicit constexpr RecursiveLambda(F &&f_) : f(std::forward<F>(f_)) {} template <class... Args> constexpr auto operator()(Args &&...args) const { return f(*this, std::forward<Args>(args)...); } }; template <class F> constexpr decltype(auto) rec_lambda(F &&f) { return RecursiveLambda<F>(std::forward<F>(f)); } #line 3 "library2/utility/rep.hpp" class Range { struct Iterator { int itr; constexpr Iterator(const int pos) noexcept : itr(pos) {} constexpr void operator++() noexcept { ++itr; } constexpr bool operator!=(const Iterator &other) const noexcept { return itr != other.itr; } constexpr int operator*() const noexcept { return itr; } }; const Iterator first, last; public: explicit constexpr Range(const int f, const int l) noexcept : first(f), last(std::max(f, l)) {} constexpr Iterator begin() const noexcept { return first; } constexpr Iterator end() const noexcept { return last; } }; constexpr Range rep(const int l, const int r) noexcept { return Range(l, r); } constexpr Range rep(const int n) noexcept { return Range(0, n); } #line 3 "library2/utility/scan.hpp" template <typename T = int> T scan() { T ret; std::cin >> ret; return ret; } #line 7 "paper.cpp" int main() { const int N = scan(); std::vector<std::vector<int>> Tree(N); std::vector<int> A(N - 1), B(N - 1); for (const int i : rep(N - 1)) { A[i] = scan() - 1; B[i] = scan() - 1; Tree[A[i]].push_back(B[i]); Tree[B[i]].push_back(A[i]); } int revsum = 0; std::vector<int> prm(N); auto set = rec_lambda([&](auto &&f, const int v, const int p) -> void { for (const int to : Tree[v]) { if (to == p) { continue; } if (v > to) { ++revsum; --prm[to]; } else { ++prm[to]; } f(to, v); } }); set(0, N); auto calc = rec_lambda([&](auto &&f, const int v, const int p) -> void { for (const int to : Tree[v]) { if (to == p) { continue; } prm[to] += prm[v]; f(to, v); } }); calc(0, N); for (const int i : rep(0, N)) { std::cout << revsum + prm[i] << std::endl; } }