結果
| 問題 | 
                            No.1098 LCAs
                             | 
                    
| コンテスト | |
| ユーザー | 
                             totori_nyaa
                         | 
                    
| 提出日時 | 2020-06-26 21:29:07 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 259 ms / 2,000 ms | 
| コード長 | 1,145 bytes | 
| コンパイル時間 | 2,214 ms | 
| コンパイル使用メモリ | 196,080 KB | 
| 最終ジャッジ日時 | 2025-01-11 10:49:21 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 28 | 
ソースコード
#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
long double eps = 1e-6;
long double pi = acos(-1);
vector<vector<int>> v(200020);
ll ans[200020];
ll dfs(int p,int pre){
    ll ret = 1;
    for(auto i:v[p]){
        if(i != pre){
            ll cnt = dfs(i,p);
            ret += cnt;
        }
    }
    ans[p] = ret*(ret);
    return ret;
}
ll dfs2(int p,int pre){
    ll ret = ans[p];
    ll cnt = 0;
    for(auto i:v[p]){
        if(i!=pre){
            cnt = dfs2(i,p);
            ans[p] -= cnt;
        }
    }
    return ret;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);
    int n;
    cin>>n;
    for(int i=1;i<n;i++){
        int a,b;
        cin>>a>>b;
        a--,b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(0,-1);
    dfs2(0,-1);
    for(int i=0;i<n;i++){
        cout << ans[i] << "\n";
    }
}
            
            
            
        
            
totori_nyaa