結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー eQeeQe
提出日時 2021-03-06 01:11:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 1,268 bytes
コンパイル時間 1,436 ms
コンパイル使用メモリ 162,160 KB
実行使用メモリ 16,384 KB
最終ジャッジ日時 2024-04-16 16:53:52
合計ジャッジ時間 4,258 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,888 KB
testcase_01 AC 3 ms
5,888 KB
testcase_02 AC 4 ms
5,888 KB
testcase_03 AC 100 ms
10,848 KB
testcase_04 AC 102 ms
10,752 KB
testcase_05 AC 101 ms
10,752 KB
testcase_06 AC 102 ms
10,752 KB
testcase_07 AC 98 ms
10,752 KB
testcase_08 AC 64 ms
9,048 KB
testcase_09 AC 29 ms
7,424 KB
testcase_10 AC 31 ms
7,296 KB
testcase_11 AC 17 ms
6,656 KB
testcase_12 AC 47 ms
8,320 KB
testcase_13 AC 57 ms
8,800 KB
testcase_14 AC 63 ms
9,088 KB
testcase_15 AC 46 ms
8,192 KB
testcase_16 AC 7 ms
6,016 KB
testcase_17 AC 7 ms
6,016 KB
testcase_18 AC 89 ms
10,240 KB
testcase_19 AC 17 ms
6,656 KB
testcase_20 AC 5 ms
5,888 KB
testcase_21 AC 45 ms
7,936 KB
testcase_22 AC 37 ms
7,808 KB
testcase_23 AC 6 ms
6,016 KB
testcase_24 AC 6 ms
6,016 KB
testcase_25 AC 5 ms
5,888 KB
testcase_26 AC 6 ms
6,016 KB
testcase_27 AC 4 ms
5,888 KB
testcase_28 AC 4 ms
5,760 KB
testcase_29 AC 7 ms
6,144 KB
testcase_30 AC 6 ms
5,888 KB
testcase_31 AC 5 ms
5,888 KB
testcase_32 AC 6 ms
6,016 KB
testcase_33 AC 20 ms
7,936 KB
testcase_34 AC 97 ms
16,384 KB
testcase_35 AC 37 ms
9,984 KB
testcase_36 AC 10 ms
6,528 KB
testcase_37 AC 59 ms
10,576 KB
testcase_38 AC 58 ms
10,324 KB
testcase_39 AC 4 ms
5,888 KB
testcase_40 AC 3 ms
5,888 KB
testcase_41 AC 3 ms
5,760 KB
testcase_42 AC 3 ms
5,888 KB
testcase_43 AC 3 ms
5,760 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:1:25: warning: extra tokens at end of #include directive
    1 | #include <bits/stdc++.h>>
      |                         ^

ソースコード

diff #

#include <bits/stdc++.h>>
#define pt(sth) cout << sth << "\n"
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
typedef long long ll;
typedef pair<ll, ll> pll;
template<class T>bool chmax(T &a, const T &b) {if(a<b) {a=b; return 1;} return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a) {a=b; return 1;} return 0;}
static const ll INF=1e18;
static const ll MAX=101010;
//static const ll MOD=1e9+7;
//static const ll MOD=998244353;
//for(i=0;i<N;i++) cin>>a[i];

typedef vector<vector<vector<ll>>> v3D;
typedef vector<vector<ll>> v2D;
typedef vector<ll> v1D;


vector<ll> g[MAX];
vector<ll> nc;
vector<ll> pa;
ll dfs(ll u,ll p){
  pa[u]=p;
  if(g[u].size()==1) return nc[u]=1;
  
  ll res=1;
  for(auto v:g[u]){
    if(v==p) continue;
    res+=dfs(v, u);
  }
  
  return nc[u]=res;
}

int main() {
  ll i, j, k;
  
  ll N;cin>>N;
  nc=vector<ll>(N,0);
  pa=vector<ll>(N,0);
  
  for(i=0;i<N-1;i++){
    ll a,b;cin>>a>>b;a--;b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  
  g[0].push_back(N);
  g[N].push_back(0);
  dfs(0,N);
  
  ll ans=0;
  for(ll u=0;u<N;u++){
    for(auto v:g[u]){
      if(v==pa[u]){
        ans+=(N-nc[u]+1)*nc[u];
      }else{
        ans+=(nc[v]+1)*(N-nc[v]);
      }
    }
  }
  pt(ans);
  
  
}



0