結果

問題 No.1418 Sum of Sum of Subtree Size
コンテスト
ユーザー eQe
提出日時 2025-11-18 08:02:56
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 4,570 bytes
コンパイル時間 3,783 ms
コンパイル使用メモリ 301,916 KB
実行使用メモリ 23,612 KB
最終ジャッジ日時 2025-11-18 08:03:04
合計ジャッジ時間 7,920 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#if __has_include(<atcoder/all>)
#endif
using namespace std;
#define eb emplace_back
#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)
#define RDVV(T,n,...) vec<T>__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resizes(n);vin(__VA_ARGS__)
#define VV(n,...) RDVV(ll,n,__VA_ARGS__)
#define fo(i,...) for(auto[i,i##stop,i##step]=for_range<ll>(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a)
#define ef(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):ranges::reverse_view(a))
#define defpp template<ostream&o=cout>void pp(const auto&...a){[[maybe_unused]]const char*c="";((o<<c<<a,c=" "),...);o<<'\n';}void epp(const auto&...a){pp<cerr>(a...);}
#define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{
namespace my{
void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}
using ll=long long;
constexpr auto refs(auto&...a){return array{ref(a)...};}
template<class T>constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};}
void lin(auto&...a){(cin>>...>>a);}
void vin(auto&...a){fo(i,(a.size()&...))(cin>>...>>a[i]);}
void dec(auto&...a){((--a),...);}
template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>;
}
namespace my{
template<class V>constexpr int depth=0;
template<class T>struct core_t_helper{using type=T;};
template<class T>using core_t=core_t_helper<T>::type;
template<class V>struct vec;
template<int D,class T>struct hvec_helper{using type=vec<typename hvec_helper<D-1,T>::type>;};
template<class T>struct hvec_helper<0,T>{using type=T;};
template<int D,class T>using hvec=hvec_helper<D,T>::type;
template<class V>struct vec:vector<V>{
  static constexpr int D=depth<V>+1;
  using C=core_t<V>;
  using vector<V>::vector;
  void resizes(const auto&...a){if constexpr(sizeof...(a)==D)*this=make(a...,C{});else{ }}
  static auto make(ll n,const auto&...a){
    if constexpr(sizeof...(a)==1)return vec<C>(n,array{a...}[0]);
    else { }
  }
  vec&operator--(){fe(*this,e)--e;return*this;}
  ll size()const{return vector<V>::size();}
  auto&emplace_back(auto&&...a){vector<V>::emplace_back(std::forward<decltype(a)>(a)...);return*this;}
};
template<class...A>requires(sizeof...(A)>=2)vec(const A&...a)->vec<hvec<sizeof...(A)-2,pack_back_t<A...>>>;
}
namespace my{
template<class T>struct queue:std::queue<T>{
  queue()=default;
  queue(const initializer_list<T>&a){fe(a,e)this->emplace(e);}
  ll size()const{return std::queue<T>::size();}
  T pop(){T r=this->front();std::queue<T>::pop();return r;}
};
}
namespace my{
template<class WT>struct edge{
  int from_,to_;
  WT wt_;
  int id_;
  edge()=default;
  edge(int from,int to,WT wt=1,int id=-1):from_(from),to_(to),wt_(wt),id_(id){}
};
}
namespace my{
template<class WT>class RootedTree{
public:
  ll n_;
  vec<vec<edge<WT>>>edges_;
  vec<edge<WT>>rev_edge_;
  int root_;
  RootedTree(ll n,ll r):n_(n),edges_(n),rev_edge_(n,{-1,-1,WT{},-1}),root_(r){}
  decltype(auto)operator[](ll i){return edges_[i];}
  ll size()const{return n_;}
  auto par(ll u)const{return u==root_?-1:rev_edge_[u].from_;}
  void set_edge(const edge<WT>&e){
    edges_[e.from_].eb(e);
    rev_edge_[e.to_]=e;
  }
  auto subtree_size_enumerate()const{
    vec<ll>res(size(),1);
    ef(bfs_order(root_),u)if(ll p=par(u);p!=-1)res[p]+=res[u];
    return res;
  }
  auto bfs_order(ll start=-1)const{
    if(start==-1)start=root_;
    vec<ll>ord;
    queue<ll>q{start};
    while(q.size()){
      ll u=q.pop();
      ord.eb(u);
      fe(edges_[u],e)q.emplace(e.to_);
    }
    return ord;
  }
};
}
namespace my{
template<class WT>class Graph{
public:
  ll n_;
  vec<vec<edge<WT>>>edges_;
  Graph()=default;
  Graph(ll n):n_(n),edges_(n){}
  auto&add_edges(const vec<ll>&a,const vec<ll>&b){fo(i,a.size())edges_[a[i]].eb(a[i],b[i],1,i);return*this;}
  vec<int>buf;
  int count=0;
  auto bfs_tree(int s)const{
    RootedTree<WT>tree(n_,s);

    vec<bool>used(n_);
    queue<edge<WT>>q{{-1,s,WT{},-1}};
    while(q.size()){
      auto pu=q.pop();
      auto u=pu.to_;
      if(used[u])continue;
      used[u]=1;

      if(pu.from_!=-1)tree.set_edge(pu);

      fe(edges_[u],e)if(!used[e.to_])q.emplace(e);
    }
    return tree;
  }
};
}
namespace my{entry
void main(){
  LL(N);
  VV(N-1,a,b);dec(a,b);
  Graph<ll>tmp(N);
  auto g=tmp.add_edges(a,b).add_edges(b,a).bfs_tree(0);

  auto sz=g.subtree_size_enumerate();

  ll ans=0;
  fo(u,N){
    ans+=sz[u]*(N-sz[u]+1);
    fe(g[u],e){
      ans+=(sz[e.to_]+1)*(N-sz[e.to_]);
    }
  }
  pp(ans);
}}
0