結果
| 問題 | No.1418 Sum of Sum of Subtree Size | 
| コンテスト | |
| ユーザー |  Nachia | 
| 提出日時 | 2021-03-05 22:15:58 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 80 ms / 2,000 ms | 
| コード長 | 911 bytes | 
| コンパイル時間 | 5,156 ms | 
| コンパイル使用メモリ | 254,980 KB | 
| 最終ジャッジ日時 | 2025-01-19 11:17:26 | 
| ジャッジサーバーID (参考情報) | judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 41 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:16:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   16 |   scanf("%d",&N);
      |   ~~~~~^~~~~~~~~
main.cpp:19:19: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   19 |     int u,v; scanf("%d%d",&u,&v); u--; v--;
      |              ~~~~~^~~~~~~~~~~~~~
            
            ソースコード
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)
using mll=atcoder::static_modint<1000000007>;
int N;
vector<vector<int>> E;
vector<int> gI,gP;
vector<ll> Z;
int main(){
  scanf("%d",&N);
  E.resize(N);
  rep(i,N-1){
    int u,v; scanf("%d%d",&u,&v); u--; v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }
  gP.assign(N,-1);
  {
    queue<int> Q; Q.push(0);
    while(Q.size()){
      int p=Q.front(); Q.pop();
      gI.push_back(p);
      for(int e:E[p]) if(e!=gP[p]){
        gP[e]=p;
        Q.push(e);
      }
    }
  }
  Z.assign(N,1);
  for(int i=N-1; i>=0; i--){
    int p=gI[i];
    for(int e:E[p]) if(gP[p]!=e) Z[p]+=Z[e];
  }
  ll ans=0;
  rep(p,N){
    ans+=N;
    ans+=Z[p]*(N-Z[p]);
    for(int e:E[p]) if(gP[p]!=e) ans+=(N-Z[e])*Z[e];
  }
  printf("%lld\n",ans);
  return 0;
}
            
            
            
        