結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-07-03 22:21:29 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,198 ms / 3,000 ms | 
| コード長 | 950 bytes | 
| コンパイル時間 | 1,671 ms | 
| コンパイル使用メモリ | 173,412 KB | 
| 実行使用メモリ | 124,308 KB | 
| 最終ジャッジ日時 | 2024-09-17 03:13:29 | 
| 合計ジャッジ時間 | 12,404 ms | 
| ジャッジサーバーID (参考情報) | judge6 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 22 | 
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
ll n = 1000005;
vector<vector<int>> tree(n,vector<int>());
ll ans = 0;
ll dfs(int i,ll depth=1,int before=-1){
    ll res = 0;
    for(int c:tree[i]){
        if(c==before) continue;
        ll p = dfs(c,depth+1,i);
        ans = (ans + (depth*p)%MOD)%MOD;
        res += p;
    }
    return res+1;
}
int main(){
    cin >> n;
    vector<int> cnt(n);
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        --a;--b;
        tree[a].push_back(b);
        tree[b].push_back(a);
        cnt[b] ++;
    }
    int id = -1;
    rep(i,n){
        if(cnt[i]==0) id = i;
    }
    dfs(id);
    cout << ans << endl;
    return 0;
}
            
            
            
        