結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,760 KB
testcase_01 AC 4 ms
5,760 KB
testcase_02 AC 3 ms
5,888 KB
testcase_03 AC 103 ms
10,624 KB
testcase_04 AC 104 ms
10,752 KB
testcase_05 AC 103 ms
10,624 KB
testcase_06 AC 105 ms
10,752 KB
testcase_07 AC 103 ms
10,752 KB
testcase_08 AC 67 ms
8,960 KB
testcase_09 AC 29 ms
7,296 KB
testcase_10 AC 34 ms
7,424 KB
testcase_11 AC 19 ms
6,596 KB
testcase_12 AC 53 ms
8,320 KB
testcase_13 AC 61 ms
8,704 KB
testcase_14 AC 63 ms
8,960 KB
testcase_15 AC 49 ms
8,320 KB
testcase_16 AC 8 ms
6,016 KB
testcase_17 AC 8 ms
6,144 KB
testcase_18 AC 89 ms
10,240 KB
testcase_19 AC 17 ms
6,532 KB
testcase_20 AC 5 ms
5,888 KB
testcase_21 AC 44 ms
7,936 KB
testcase_22 AC 39 ms
7,680 KB
testcase_23 AC 6 ms
5,888 KB
testcase_24 AC 6 ms
6,016 KB
testcase_25 AC 5 ms
5,888 KB
testcase_26 AC 6 ms
5,888 KB
testcase_27 AC 4 ms
5,760 KB
testcase_28 AC 4 ms
5,888 KB
testcase_29 AC 7 ms
5,888 KB
testcase_30 AC 6 ms
6,016 KB
testcase_31 AC 5 ms
5,760 KB
testcase_32 AC 6 ms
5,888 KB
testcase_33 AC 22 ms
8,064 KB
testcase_34 AC 104 ms
16,384 KB
testcase_35 AC 39 ms
9,984 KB
testcase_36 AC 10 ms
6,528 KB
testcase_37 AC 65 ms
10,708 KB
testcase_38 AC 59 ms
10,204 KB
testcase_39 AC 4 ms
5,632 KB
testcase_40 AC 3 ms
5,888 KB
testcase_41 AC 3 ms
5,888 KB
testcase_42 AC 3 ms
5,760 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(u>0 and 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);
  }
  
  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])*nc[u];
      }else{
        ans+=nc[v]*(N-nc[v]);
      }
    }
  }
  pt(ans+N*N);
  
  
}



0