結果
問題 |
No.277 根掘り葉掘り
|
ユーザー |
|
提出日時 | 2024-12-06 22:36:16 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,397 bytes |
コンパイル時間 | 3,092 ms |
コンパイル使用メモリ | 251,516 KB |
実行使用メモリ | 13,824 KB |
最終ジャッジ日時 | 2024-12-06 22:36:21 |
合計ジャッジ時間 | 4,705 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 10 WA * 8 |
ソースコード
#ifndef LOCAL #include <bits/stdc++.h> using namespace std; #define debug(...) (void(0)) #else #include "algo/debug.h" #endif namespace std { template <class Fun> class y_combinator_result { Fun fun_; public: template <class T> explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {} template <class... Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); } }; template <class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); } } // namespace std void solve() { int N; cin >> N; vector<vector<int>> G(N); for (int i = 1; i < N; i++) { int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); G[b].push_back(a); } vector<int> L(N, 1e9); vector<int> R(N); y_combinator([&](auto self, int v, int p = -1) -> void { for(const auto&to: G[v]) if(to != p) { R[to] = R[v] + 1; self(to, v); L[v] = min(L[v], L[to] + 1); } if(L[v] == (int)1e9) L[v] = 0; })(0); for(int i = 0; i < N; i++) cout << min(R[i], L[i]) << "\n"; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int tt = 1; // std::cin >> tt; while (tt--) { solve(); } }