結果
| 問題 | 
                            No.1098 LCAs
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-06-26 22:48:22 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 175 ms / 2,000 ms | 
| コード長 | 1,095 bytes | 
| コンパイル時間 | 948 ms | 
| コンパイル使用メモリ | 87,140 KB | 
| 実行使用メモリ | 34,708 KB | 
| 最終ジャッジ日時 | 2024-07-04 22:49:52 | 
| 合計ジャッジ時間 | 4,249 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 28 | 
ソースコード
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
#define int long long
#define endl "\n"
constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 
struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;
vector<vector<int>> tree;
vector<int> con, ans;
void solve(int cur = 0, int per = -1){
	int sum = 0;
	
	con[cur] = 1;
	
	for(int nex : tree[cur]){
		if(nex == per) continue;
		
		solve(nex, cur);
		
		con[cur] += con[nex];
		sum += con[nex];
	}
	
	ans[cur] += (sum + 1) * 2 - 1;
	
	for(int nex : tree[cur]){
		if(nex == per) continue;
		ans[cur] += (sum - con[nex]) * con[nex]; 
	}
}
signed main(){
	cout<<fixed<<setprecision(10);
	
	int N;
	
	cin>>N;
	
	tree.resize(N);
	con.resize(N);
	ans.resize(N);
	
	for(int i = 0; i < N-1; i++){
		int u, w;
		
		cin>>u>>w;
		
		u--, w--;
		
		tree[u].push_back(w);
		tree[w].push_back(u);
	}
	
	solve();
	
	for(int i = 0; i < N; i++){
		cout<<ans[i]<<endl;
	}
	
	return 0;
}