結果
| 問題 | No.1418 Sum of Sum of Subtree Size | 
| コンテスト | |
| ユーザー |  沙耶花 | 
| 提出日時 | 2021-03-05 21:44:33 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 58 ms / 2,000 ms | 
| コード長 | 721 bytes | 
| コンパイル時間 | 3,700 ms | 
| コンパイル使用メモリ | 253,976 KB | 
| 最終ジャッジ日時 | 2025-01-19 10:50:16 | 
| ジャッジサーバーID (参考情報) | judge3 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 41 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:35:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   35 |                 scanf("%d %d",&u,&v);
      |                 ~~~~~^~~~~~~~~~~~~~~
            
            ソースコード
#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint1000000007;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001
long long ans = 0LL;
int n;
vector<long long> sz;
vector<vector<int>> E;
void dfs(int cur,int p){
	sz[cur] = 1;
	ans += n;
	rep(i,E[cur].size()){
		int to = E[cur][i];
		if(to==p)continue;
		dfs(to,cur);
		ans += sz[to] * (n-sz[to]);
		sz[cur] += sz[to];
	}
	ans += (n-sz[cur]) * sz[cur];
}
int main(){
	
	
	cin>>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);
	}
	sz.resize(n,0);
	
	dfs(0,-1);
	
	cout<<ans<<endl;
	
    return 0;
}
            
            
            
        