Language: C++

#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;

#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using P = pair<ll, ll>;

int main(void) {
    ll n; cin >> n;
    vvll g(n);
    rep(i, n-1) {
        ll u, v; cin >> u >> v; u--, v--;
        g[u].pb(v), g[v].pb(u);
    }

    vll d(n);
    function<ll(ll, ll)> f = [&](ll v, ll p) {
        for (auto u : g[v]) if (u != p) chmax(d[v], f(u, v));
        return d[v] + 1;
    };
    f(0, -1);

    vll ret(n);
    // dpar[v][p] = p???v???????????¨?????¨????????§?????§???d
    function<void(ll, ll, ll)> ff = [&](ll v, ll p, ll dpar) {
        vector<P> children;
        for (auto u : g[v]) children.emplace_back(u == p ? dpar + 1 : d[u] + 1, u);
        sort(all(children)); reverse(all(children));

        ret[v] = children[0].fi;

        for (auto u : g[v]) if (u != p) ff(u, v, children[children[0].se==u].fi);
    };
    ff(0, -1, 0);

    rep(i, n) cout << 2 * (n - 1) - ret[i] << endl;


    return 0;
}
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;

#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using P = pair<ll, ll>;

int main(void) {
    ll n; cin >> n;
    vvll g(n);
    rep(i, n-1) {
        ll u, v; cin >> u >> v; u--, v--;
        g[u].pb(v), g[v].pb(u);
    }

    vll d(n);
    function<ll(ll, ll)> f = [&](ll v, ll p) {
        for (auto u : g[v]) if (u != p) chmax(d[v], f(u, v));
        return d[v] + 1;
    };
    f(0, -1);

    vll ret(n);
    // dpar[v][p] = p???v???????????¨?????¨????????§?????§???d
    function<void(ll, ll, ll)> ff = [&](ll v, ll p, ll dpar) {
        vector<P> children;
        for (auto u : g[v]) children.emplace_back(u == p ? dpar + 1 : d[u] + 1, u);
        sort(all(children)); reverse(all(children));

        ret[v] = children[0].fi;

        for (auto u : g[v]) if (u != p) ff(u, v, children[children[0].se==u].fi);
    };
    ff(0, -1, 0);

    rep(i, n) cout << 2 * (n - 1) - ret[i] << endl;


    return 0;
}

Compiler output:
a.cc:57:35: error: redefinition of 'template<class T1, class T2> bool chmin(T1&, T2)'
   57 | template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
      |                                   ^~~~~
a.cc:10:35: note: 'template<class T1, class T2> bool chmin(T1&, T2)' previously declared here
   10 | template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
      |                                   ^~~~~
a.cc:58:35: error: redefinition of 'template<class T1, class T2> bool chmax(T1&, T2)'
   58 | template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
      |                                   ^~~~~
a.cc:11:35: note: 'template<class T1, class T2> bool chmax(T1&, T2)' previously declared here
   11 | template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
      |                                   ^~~~~
a.cc:62:5: error: redefinition of 'int main()'
   62 | int main(void) {
      |     ^~~~
a.cc:15:5: note: 'int main()' previously defined here
   15 | int main(void) {
      |     ^~~~
In file included from /usr/include/c++/14/functional:59,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/std_function.h: In constructor 'std::function<_Res(_ArgTypes ...)>::function(_Functor&&) [with _Functor = main()::<lambda(ll, ll, ll)>; _Constraints = void; _Res = void; _ArgTypes = {long long int, long long int, long long int}]':
/usr/include/c++/14/bits/std_function.h:435:9: error: mangling of 'std::function<_Res(_ArgTypes ...)>::function(_Functor&&) [with _Functor = main()::<lambda(ll, ll, ll)>; _Constraints = void; _Res = void; _ArgTypes = {long long int, long long int, long long int}]' as '_ZNSt8functionIFvxxxEEC2IZ4mainEUlxxxE_vEEOT_' conflicts with a previous mangle
  435 |         function(_Functor&& __f)
      |         ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:435:9: note: previous mangling 'std::function<_Res(_ArgTypes ...)>::function(_Functor&&) [with _Functor = main()::<lambda(ll, ll, ll)>; _Constraints = void; _Res = void; _ArgTypes = {long long int, long long int, long long int}]'
/usr/include/c++/14/bits/std_function.h:435:9: note: a later '-fabi-version=' (or =0) avoids this error with a change in mangling
/usr/include/c++/14/bits/std_function.h: In static member function 'static bool std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_manager(std::_Any_data&, const std::_Any_data&, std::_Manager_operation) [with _Res = void; _Functor = main()::<lambda(ll, ll, ll)>; _ArgTypes = {long long int, long long int, long long int}]':
/usr/include/c++/14/bits/std_function.h:267:7: error: mangling of 'static bool std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_manager(std::_Any_data&, const std::_Any_data&, std::_Manager_operation) [with _Res = void; _Functor = main()::<lambda(ll, ll, ll)>; _ArgTypes = {long long int, long long int, long long int}]' as '_ZNSt17_Function_handlerIFvxxxEZ4mainEUlxxxE_E10_M_managerERSt9_Any_dataRKS3_St18_Manager_operation' conflicts with a previous mangle
  267 |       _M_manager(_Any_data& __dest, const _Any_data& __source,
      |       ^~~~~~~~~~
/usr/include/c++/14/bits/std_function.h:267:7: note: previous mangling 'static bool std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_manager(std::_Any_data&, const std::_Any_data&, std::_Manager_operation) [with _Res = void; _Functor = main()::<lambda(ll, ll, ll)>; _ArgTypes = {long long int, long long int, long long int}]'
/usr/include/c++/14/bits/std_function.h:267:7: note: a later '-fabi-version=' (or =0) avoids this error with a change in mangling
/usr/include/c++/14/bits/std_function.h: In static member function 'static _Functor* std::_Function_base::_Base_manager<_Functor>::_M_get_pointer(const std::_Any_data&) [with _Functor = main()::<lambda(ll, ll, ll)>]':
/usr/include/c++/14/bits/std_function.h:134:9: error: mangling of 'static _Functor* std::_Function_base::_Base_manager<_Functor>::_M_get_pointer(const std::_Any_data&) [with _Functor = main()::<lambda(ll, ll, ll)>]' as '_ZNSt14_Function_base13_Base_managerIZ4mainEUlxxxE_E14_M_get_pointerERKSt9_Any_data' conflicts with a previous mangle
  134 |         _M_get_pointer(const _Any_data& __source) noexcept
      |         ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/std_function.h:134:9: note: previous mangling 'static _Functor* std::_Function_base::_Base_manager<_Functor>::_M_get_pointer(const std::_Any_data&) [with _Functor = main()::<lambda(ll, ll, ll)>]'
/usr/include/c++/14/bits/std_function.h:134:9: note: a later '-fabi-version=' (or =0) avoids this error with a change in mangling
/usr/include/c++/14/bits/std_function.h: In member function 'const _Tp& std::_Any_data::_M_access() const [with _Tp = main()::<lambda(ll, ll, ll)>*]':
/usr/include/c++/14/bits/std_function.h:95:7: error: mangling of 'const _Tp& std::_Any_data::_M_access() const [with _Tp = main()::<lambda(ll, ll, ll)>*]' as '_ZNKSt9_Any_data9_M_accessIPZ4mainEUlxxxE_EERKT_v' conflicts with a previous mangle
   95 |       _M_access() const noexcept
      |       ^~~~~~~~~
/usr/include/c++/14/bits/std_function.h:95:7: note: previous mangling 'const _Tp& std::_Any_data::_M_access() const [with _Tp = main()::<lambda(ll, ll, ll)>*]'
/usr/include/c++/14/bits/std_function.h:95:7: note: a later '-fabi-version=' (or =0) avoids this error with a change in mangling
/usr/include/c++/14/bits/std_function.h: In member function '_Tp& std::_Any_data::_M_access() [with _Tp = main()::<lambda(ll, ll, ll)>*]':
/usr/include/c++/14/bits/std_function.h:90:7: error: mangling of '_Tp& std::_Any_data::_M_access() [with _Tp = main()::<lambda(ll, ll, ll)>*]' as '_ZNSt9_Any_data9_M_accessIPZ4mainEUlxxxE_EERT_v' conflicts with a previous mangle
   90 |       _M_access() noexcept
      |       ^~~~~~~~~
/usr/include/c++/14/bits/std_function.h:90:7: note: previous mangling '_Tp& std::_Any_data::_M_access() [with _Tp = main()::<lambda(ll, ll, ll)>*]'
/usr/include/c++/14/bits/std_function.h:90:7: note: a later '-fabi-version=' (or =0) avoids this error with a change in mangling
/usr/include/c++/14/bits/std_function.h: In static member function 'static bool std::_Function_base::_Base_manager<_Functor>::_M_manager(std::_Any_data&, const std::_Any_data&, std::_Manager_operation) [with _Functor = main()::<lambda(ll, ll, ll)>]':
/usr/include/c++/14/bits/std_function.h:180:9: error: mangling of 'static bool std::_Function_base::_Base_manager<_Functor>::_M_manager(std::_Any_data&, const std::_Any_data&, std::_Manager_operation) [with _Functor = main()::<lambda(ll, ll, ll)>]' as '_ZNSt14_Function_base13_Base_managerIZ4mainEUlxxxE_E10_M_managerERSt9_Any_dataRKS3_St18_Manager_operation' conflicts with a previous mangle
  180 |         _M_manager(_Any_data& __dest, const _Any_data& __source,
      |         ^~~~~~~~~~
/usr/include/c++/14/bits/std_function.h:180:9: note: previous mangling 'static bool std::_Function_base::_Base_manager<_Functor>::_M_manager(std::_Any_data&, const std::_Any_data&, std::_Manager_operation) [with _Functor = main()::<lambda(ll, ll, ll)>]'
/usr/include/c++/14/bits/std_function.h:180:9: note: a later '-fabi-version=' (or =0) avoids this error with a change in mangling
/usr/include/c++/14/bits/std_function.h: In static member function 'static void std::_Function_base::_Base_manager<_Functor>::_M_init_functor(std::_Any_data&, _Fn&&) [with _Fn = const main()::<lambda(ll, ll, ll)>&; _Functor = main()::<lambda(ll, ll, ll)>]':
/usr/include/c++/14/bits/std_function.h:211:11: error: mangling of 'static void std::_Function_base::_Base_manager<_Functor>::_M_init_functor(std::_Any_data&, _Fn&&) [with _Fn = const main()::<lambda(ll, ll, ll)>&; _Functor = main()::<lambda(ll, ll, ll)>]' as '_ZNSt14_Function_base13_Base_managerIZ4mainEUlxxxE_E15_M_init_functorIRKS1_EEvRSt9_Any_dataOT_' conflicts with a previous mangle
  211 |           _M_init_functor(_Any_data& __functor, _Fn&& __f)
      |           ^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/std_function.h:211:11: note: previous mangling 'static void std::_Function_base::_Base_manager<_Functor>::_M_init_functor(std::_Any_data&, _Fn&&) [with _Fn = const main()::<lambda(ll, ll, ll)>&; _Functor = main()::<lambda(ll, ll, ll)>]'
/usr/include/c++/14/bits/std_function.h:211:11: note: a later '-fabi-version=' (or =0) avoids this error with a change in mangling
In file included from /usr/include/c++/14/bits/stl_pair.h:61,
                 from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/move.h: In function 'constexpr _Tp&& std::forward(typename remove_reference<_Functor>::type&) [with _Tp = const main()::<lambda(ll, ll, ll)>&]':
/usr/include/c++/14/bits/move.h:71:5: error: mangling of 'constexpr _Tp&& std::forward(typename remove_reference<_Functor>::type&) [with _Tp = const main()::<lambda(ll, ll, ll)>&]' as '_ZSt7forwardIRKZ4mainEUlxxxE_EOT_RNSt16remove_referenceIS3_E4typeE' conflicts with a previous mangle
   71 |     forward(typename std::remove_reference<_Tp>::type& __t) noexcept
      |     ^~~~~~~
/usr/include/c++/14/bits/move.h:71:5: note: previous mangling 'constexpr _Tp&& std::forward(typename remove_reference<_Functor>::type&) [with _Tp = const main()::<lambda(ll, ll, ll)>&]'
/usr/include/c++/14/bits/move.h:71:5: note: a later '-fabi-version=' (or =0) avoids this error with a change in mangling
/usr/include/c++/14/bits/std_function.h: In static member function 'static void std::_Function_base::_Base_manager<_Functor>::_M_create(std::_Any_data&, _Fn&&, std::false_type) [with _Fn = const main()::<lambda(ll, ll, ll)>&; _Functor = main()::<lambda(ll, ll, ll)>]':
/usr/include/c++/14/bits/std_function.h:158:11: error: mangling of 'static void std::_Function_base::_Base_manager<_Functor>::_M_create(std::_Any_data&, _Fn&&, std::false_type) [with _Fn = const main()::<lambda(ll, ll, ll)>&; _Functor = main()::<lambda(ll, ll, ll)>]' as '_ZNSt14_Function_base13_Base_managerIZ4mainEUlxxxE_E9_M_createIRKS1_EEvRSt9_Any_dataOT_St17integral_constantIbLb0EE' conflicts with a p