結果
問題 | No.1418 Sum of Sum of Subtree Size |
ユーザー | logx |
提出日時 | 2021-03-05 22:16:16 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 68 ms / 2,000 ms |
コード長 | 5,604 bytes |
コンパイル時間 | 2,051 ms |
コンパイル使用メモリ | 202,724 KB |
最終ジャッジ日時 | 2025-01-19 11:17:38 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
ソースコード
#ifdef LOGX #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; //#include <atcoder/all> //using namespace atcoder; /*---------macro---------*/ #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rep2(i, s, n) for (int i = s; i < (int)(n); ++i) #define Clear(a) a = decltype(a)() #define all(a) begin(a),end(a) #define rall(a) rbegin(a),rend(a) #define bit(i,j) (((i)>>(j))&1) /*---------type/const---------*/ typedef long long ll; typedef unsigned long long ull; typedef string::const_iterator state; //構文解析 const ll big=1000000007; //const ll big=998244353; const double PI=acos(-1); const double EPS=1e-8;//適宜変える const int dx[4]={1,0,-1,0}; const int dy[4]={0,1,0,-1}; const char newl='\n'; struct{ constexpr operator int(){return -int(1e9)-1;} constexpr operator ll(){return -ll(1e18)-1;} }neginf; struct{ constexpr operator int(){return int(1e9)+1;} constexpr operator ll(){return ll(1e18)+1;} constexpr auto operator -(){return neginf;} }inf; /*---------debug用関数---------*/ #ifdef LOGX #define dbg(x) cout << #x << ':',prt(x); template<typename T> inline void prt(T a_); template<typename T> inline void _prt(T a_); template<typename A,typename B> inline void _prt(map<A,B> a_); template<typename A,typename B> inline void _prt(unordered_map<A,B> a_); template<typename T> inline void _prt(set<T> a_); template<typename T> inline void _prt(unordered_set<T> a_); template<typename A,typename B> inline void _prt(pair<A,B> a_); template<typename T> inline void _prt(vector<T> &a_); template<typename T> inline void _prt(vector<vector<T>> &a_); //_prtでは改行しない. inline void _prt(bool a_){if(a_)cout<<1;else cout<<0;} inline void _prt(int a_){if(a_>=(int)inf)cout << "inf";else if(a_<=-(int)inf)cout << "-inf";else cout << a_;} inline void _prt(long long a_){if(a_>=(ll)inf)cout << "INF";else if(a_<=-(ll)inf)cout << "-INF";else cout << a_;} #ifdef ATCODER_MODINT_HPP template<int MOD>inline void _prt(static_modint<MOD> x){cout << x.val();} template<int MOD>inline void _prt(dynamic_modint<MOD> x){cout << x.val();} #endif template<typename T> inline void _prt(T a_){cout << a_;} template<typename A,typename B> inline void _prt(map<A,B> a_){cout<<'{';auto it=a_.begin();while(it!=a_.end()){_prt((*it).first);cout<<':';_prt((*(it++)).second);cout<<(it==a_.end() ? "":",");}cout<<'}';} template<typename A,typename B> inline void _prt(unordered_map<A,B> a_){cout<<'{';auto it=a_.begin();while(it!=a_.end()){_prt((*it).first);cout<<':';_prt((*(it++)).second);cout<<(it==a_.end() ? "":",");}cout<<'}';} template<typename T> inline void _prt(set<T> a_){cout<<'{';auto it=a_.begin();while(it!=a_.end()){_prt(*it++);cout<<(it==a_.end() ? "":",");}cout<<'}';} template<typename T> inline void _prt(unordered_set<T> a_){cout<<'{';auto it=a_.begin();while(it!=a_.end()){_prt(*it++);cout<<(it==a_.end() ? "":",");}cout<<'}';} template<typename A,typename B> inline void _prt(pair<A,B> a_){cout << '(';_prt(a_.first);cout << ',';_prt(a_.second);cout << ')';} template<typename T> inline void _prt(vector<T> &a_){cout<<'{';for(int i=0;i<(int)a_.size();i++){_prt(a_[i]);if(i+1<(int)a_.size())cout<<',';}cout<<'}';} template<typename T> inline void _prt(vector<vector<T>> &a_){cout<<"{\n";for(int i=0;i<(int)a_.size();i++){_prt(a_[i]);if(i+1<(int)a_.size())cout<<",\n";else cout<<"\n}";}} template<typename T> inline void prt(T a_){_prt(a_);cout<<'\n';} #else #define dbg(x) ; #define prt(x) ; #define _prt(x) ; #endif /*---------function---------*/ template<typename T,typename U> T max(const T x,const U y){if(x>y)return x;else return y;} template<typename T,typename U> T min(const T x,const U y){if(x<y)return x;else return y;} template<typename T> T max(const vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;} template<typename T> T min(const vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;} template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;} template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;} template<typename T,typename U> T expm(T x,U y,const ll mod=big){ T res=1; while(y){ if(y&1)(res*=x)%=mod; (x*=x)%=mod; y>>=1; } return res; } template<typename T,typename U> T exp(T x,U y){ T res=1; while(y){ if(y&1)res*=x; x*=x; y>>=1; } return res; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); cout.precision(10); /*--------------------------------*/ int N;cin >> N; vector<vector<int>> g(N); vector<vector<int>> sz(N); rep(i,N-1){ int a,b;cin >> a >> b; g[a-1].emplace_back(b-1); g[b-1].emplace_back(a-1); } rep(i,N){ sz[i].resize(g[i].size()); } auto dfs=[&](auto &Self,int i,int p=-1)->int{ int res=0; rep(j,g[i].size()){ if(g[i][j]==p)continue; res+=sz[i][j]=Self(Self, g[i][j], i); } return res+1; }; dfs(dfs, 0); auto dfs2=[&](auto &Self,int i,int p=-1,int val=0)->void{ rep(j,g[i].size()){ if(g[i][j]==p)sz[i][j]=val; } int sum=0; rep(j,g[i].size()){ sum+=sz[i][j]; } rep(j,g[i].size()){ if(g[i][j]==p)continue; Self(Self,g[i][j],i,sum-sz[i][j]+1); } }; dfs2(dfs2, 0); dbg(sz); ll ans=0; rep(i,N){ for(auto val:sz[i])ans+=(val+1)*ll(N-val); } ans+=N; cout << ans << newl; }