結果
| 問題 |
No.2427 Tree Distance Two
|
| コンテスト | |
| ユーザー |
poyon
|
| 提出日時 | 2023-08-18 21:13:09 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 452 ms / 2,000 ms |
| コード長 | 9,959 bytes |
| コンパイル時間 | 2,422 ms |
| コンパイル使用メモリ | 208,924 KB |
| 最終ジャッジ日時 | 2025-02-16 09:22:46 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 35 |
ソースコード
// clang-format off
#ifdef LOCAL
#include <pch.hpp>
#else
#include <bits/stdc++.h>
#define cerr if (false) cerr
#define debug_bar
#define debug(...)
#define debug2(vv)
#define debug3(vvv)
#endif
using namespace std;
using ll = long long;
using ld = long double;
using str = string;
using P = pair<ll,ll>;
using VP = vector<P>;
using VVP = vector<VP>;
using VC = vector<char>;
using VS = vector<string>;
using VVS = vector<VS>;
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using VLL = vector<ll>;
using VVLL = vector<VLL>;
using VVVLL = vector<VVLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VVVB = vector<VVB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VVVD = vector<VVD>;
#define FOR(i,l,r) for (ll i = (l); i < (r); ++i)
#define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define FORE(e,c) for (auto&& e : c)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define COUNT(c,v) count(ALL(c),(v))
#define len(c) ((ll)(c).size())
#define BIT(b,i) (((b)>>(i)) & 1)
#define PCNT(b) ((ll)__builtin_popcountll(b))
#define LB(c,v) distance((c).begin(), lower_bound(ALL(c), (v)))
#define UB(c,v) distance((c).begin(), upper_bound(ALL(c), (v)))
#define UQ(c) do { SORT(c); (c).erase(unique(ALL(c)), (c).end()); (c).shrink_to_fit(); } while (0)
#define END(...) do { print(__VA_ARGS__); exit(0); } while (0)
constexpr ld EPS = 1e-10;
constexpr ld PI = acosl(-1.0);
constexpr int inf = (1 << 30) - (1 << 15); // 1,073,709,056
constexpr ll INF = (1LL << 62) - (1LL << 31); // 4,611,686,016,279,904,256
template<class... T> void input(T&... a) { (cin >> ... >> a); }
void print() { cout << '\n'; }
template<class T> void print(const T& a) { cout << a << '\n'; }
template<class P1, class P2> void print(const pair<P1, P2>& a) { cout << a.first << " " << a.second << '\n'; }
template<class T, class... Ts> void print(const T& a, const Ts&... b) { cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; }
template<class T> void cout_line(const vector<T>& ans, int l, int r) { for (int i = l; i < r; i++) { if (i != l) { cout << ' '; } cout << ans[i]; } cout << '\n'; }
template<class T> void print(const vector<T>& a) { cout_line(a, 0, a.size()); }
template<class S, class T> bool chmin(S& a, const T b) { if (b < a) { a = b; return 1; } return 0; }
template<class S, class T> bool chmax(S& a, const T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> T SUM(const vector<T>& A) { return accumulate(ALL(A), T(0)); }
template<class T> vector<T> cumsum(const vector<T>& A, bool offset = false) { int N = A.size(); vector<T> S(N+1, 0); for (int i = 0; i < N; i++) { S[i+1] = S[i] + A[i]; } if (not offset) { S.erase(S.begin()); } return S; }
template<class T> string to_binary(T x, int B = 0) { string s; while (x) { s += ('0' + (x & 1)); x >>= 1; } while ((int)s.size() < B) { s += '0'; } reverse(s.begin(), s.end()); return s; }
template<class F> ll binary_search(const F& is_ok, ll ok, ll ng) { while (abs(ok - ng) > 1) { ll m = (ok + ng) / 2; (is_ok(m) ? ok : ng) = m; } return ok; }
template<class F> double binary_search_real(const F& is_ok, double ok, double ng, int iter = 90) { for (int i = 0; i < iter; i++) { double m = (ok + ng) / 2; (is_ok(m) ? ok : ng) = m; } return ok; }
template<class T> using PQ_max = priority_queue<T>;
template<class T> using PQ_min = priority_queue<T, vector<T>, greater<T>>;
template<class T> T pick(stack<T>& s) { assert(not s.empty()); T x = s.top(); s.pop(); return x; }
template<class T> T pick(queue<T>& q) { assert(not q.empty()); T x = q.front(); q.pop(); return x; }
template<class T> T pick_front(deque<T>& dq) { assert(not dq.empty()); T x = dq.front(); dq.pop_front(); return x; }
template<class T> T pick_back(deque<T>& dq) { assert(not dq.empty()); T x = dq.back(); dq.pop_back(); return x; }
template<class T> T pick(PQ_min<T>& pq) { assert(not pq.empty()); T x = pq.top(); pq.pop(); return x; }
template<class T> T pick(PQ_max<T>& pq) { assert(not pq.empty()); T x = pq.top(); pq.pop(); return x; }
template<class T> T pick(vector<T>& v) { assert(not v.empty()); T x = v.back(); v.pop_back(); return x; }
int to_int(const char c) { if (islower(c)) { return (c - 'a'); } if (isupper(c)) { return (c - 'A'); } if (isdigit(c)) { return (c - '0'); } assert(false); }
char to_a(const int i) { assert(0 <= i && i < 26); return ('a' + i); }
char to_A(const int i) { assert(0 <= i && i < 26); return ('A' + i); }
char to_d(const int i) { assert(0 <= i && i <= 9); return ('0' + i); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
ll max(int a, ll b) { return max((ll)a, b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll mod(ll x, ll m) { assert(m > 0); return (x % m + m) % m; }
ll ceil(ll a, ll b) { if (b < 0) { return ceil(-a, -b); } assert(b > 0); return (a < 0 ? a / b : (a + b - 1) / b); }
ll floor(ll a, ll b) { if (b < 0) { return floor(-a, -b); } assert(b > 0); return (a > 0 ? a / b : (a - b + 1) / b); }
ll powint(ll x, ll n) { assert(n >= 0); if (n == 0) { return 1; }; ll res = powint(x, n>>1); res *= res; if (n & 1) { res *= x; } return res; }
pair<ll,ll> divmod(ll a, ll b) { assert(b != 0); ll q = floor(a, b); return make_pair(q, a - q * b); }
ll bitlen(ll b) { if (b <= 0) { return 0; } return (64LL - __builtin_clzll(b)); }
ll digitlen(ll n) { assert(n >= 0); if (n == 0) { return 1; } ll sum = 0; while (n > 0) { sum++; n /= 10; } return sum; }
ll msb(ll b) { return (b <= 0 ? -1 : (63 - __builtin_clzll(b))); }
ll lsb(ll b) { return (b <= 0 ? -1 : __builtin_ctzll(b)); }
// --------------------------------------------------------
// References:
// <https://null-mn.hatenablog.com/entry/2020/04/14/124151>
// <https://qiita.com/keymoon/items/2a52f1b0fb7ef67fb89e>
// <https://hcpc-hokudai.github.io/archive/dynamic_programming_rerooting.pdf>
// <https://algo-logic.info/tree-dp/>
// <https://github.com/atcoder/ac-library/blob/master/atcoder/segtree.hpp> (for implementation)
/**
* @brief 全方位木 DP (Rerooting DP)
*
* @tparam S 可換モノイドの型 (モノイド: 結合律を満たし単位元が存在する代数構造)
* @tparam (*op)(S,S) 二項演算 (辺属性のマージ関数)
* @tparam (*fv)(S,int,bool,bool) 辺属性→頂点属性にする関数 (S,頂点番号,根か,葉か)
* @tparam (*fe)(S,int,int,ll) 頂点属性→辺属性にする関数 (S,始点番号,終点番号,重み)
* @tparam (*e)() 単位元
*/
template <class S, S (*op)(S, S), S (*fv)(S, int, bool, bool), S (*fe)(S, int, int, ll), S (*e)()>
struct rerooting {
public:
vector<vector<pair<int,ll>>> G;
rerooting(int n) : N(n) {
G.resize(N);
dp.resize(N);
ans.resize(N);
}
// 頂点 u から頂点 v に有向辺を張る
// - 無向グラフの場合は両方向を追加する必要あり
void add_edge(int u, int v, ll w) {
assert(0 <= u && u < N);
assert(0 <= v && v < N);
G[u].emplace_back(v, w);
}
void build() {
for (int u = 0; u < N; u++) { dp[u].resize(G[u].size()); }
dfs1(0, -1);
dfs2(0, -1, e());
}
// 下向きの dp[u][i] を求める
S dfs1(int u, int p) {
S dp_s = e();
int m = G[u].size();
for (int i = 0; i < m; i++) {
const auto& [v, w] = G[u][i];
if (v == p) { continue; }
dp[u][i] = dfs1(v, u);
dp_s = op(dp_s, fe(dp[u][i], u, v, w));
}
bool is_leaf = (p == -1 ? false : m == 1);
return fv(dp_s, u, false, is_leaf);
}
// 上向きの dp[u][i] (= px) を伝搬しながら ans[u] を求める
void dfs2(int u, int p, S px) {
int m = G[u].size();
// 右から累積積を前計算
vector<S> dp_R(m+1);
dp_R[m] = e();
for (int i = m-1; 0 <= i; i--) {
const auto& [v, w] = G[u][i];
if (v == p) { dp[u][i] = px; }
dp_R[i] = op(fe(dp[u][i], u, v, w), dp_R[i+1]);
}
// 頂点 u を根とした木に対する答え
ans[u] = fv(dp_R[0], u, true, false); /** NOTE: 根以外で次数が 1 の頂点を葉と定義 **/
// 左から累積積を計算しながら dfs
S dp_l = e();
bool is_leaf = (p == -1 ? m == 1 : false);
for (int i = 0; i < m; i++) {
const auto& [v, w] = G[u][i];
if (v != p) { dfs2(v, u, fv(op(dp_l, dp_R[i+1]), u, false, is_leaf)); }
dp_l = op(dp_l, fe(dp[u][i], u, v, w));
}
}
S query(int u) const noexcept {
assert(0 <= u && u < N);
return ans[u];
}
private:
int N;
vector<vector<S>> dp; // dp[u][i] := u から出る i 番目の有向辺の先の部分木に対応する値
vector<S> ans; // ans[u] := u を根とした木に対する答え
};
struct Mono {
ll n1 = 0;
ll n2 = 0;
};
Mono op(Mono a, Mono b) {
a.n1 += b.n1;
a.n2 += b.n2;
return a;
};
Mono fv(Mono x, [[maybe_unused]] int u, [[maybe_unused]] bool is_root, [[maybe_unused]] bool is_leaf) {
return x;
}
Mono fe(Mono x, [[maybe_unused]] int s, [[maybe_unused]] int t, [[maybe_unused]] ll w) {
x.n2 = x.n1;
x.n1 = 1;
return x;
};
Mono e() { return Mono{0, 0}; };
// clang-format on
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
ll N;
cin >> N;
rerooting<Mono, op, fv, fe, e> re(N);
// const vector<vector<pair<int,ll>>>& G = re.G;
REP (_, N - 1) {
int u, v;
cin >> u >> v;
u--;
v--;
re.add_edge(u, v, 0);
re.add_edge(v, u, 0);
}
re.build();
REP (u, N) { print(re.query(u).n2); }
return 0;
}
poyon