結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-07-04 01:37:48 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 991 ms / 3,000 ms | 
| コード長 | 1,202 bytes | 
| コンパイル時間 | 776 ms | 
| コンパイル使用メモリ | 81,412 KB | 
| 最終ジャッジ日時 | 2025-01-11 15:18:45 | 
| ジャッジサーバーID (参考情報) | judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 22 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:43:8: warning: ‘root’ may be used uninitialized [-Wmaybe-uninitialized]
   43 |     dfs(root,0,deep,des,child);
      |     ~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:35:9: note: ‘root’ was declared here
   35 |     int root;
      |         ^~~~
            
            ソースコード
#include<iostream>
#include<vector>
#include<utility>
using ll = long long;
using Pii = std::pair<int,int>;
const long long mod = 1000000007;
long long dfs(int now,ll dep,std::vector<long long>& deep,std::vector<long long>& des,std::vector<std::vector<long long>>& child){
    deep.at(now) = dep;
    ll res = 1;
    for(int nxt:child.at(now)){
        ll x = 0;
        x = dfs(nxt,dep+1,deep,des,child);
        res += x;
        des.at(now) += x;
    }
    return res;
}
int main(){
    ll n;
    std::cin >> n;
    std::vector child(n,std::vector<ll>());
    std::vector parent(n,true);
    std::vector AB(n-1,Pii());
    for(int i=0; i<n-1; i++){
        ll a,b;
        std::cin >> a >> b;
        AB.at(i) = {a-1,b-1};
        child.at(a-1).push_back(b-1);
        parent.at(b-1) = false;
    }
    int root;
    for(int i=0; i<n; i++){
        if(parent.at(i)){
            root = i;
            break;
        }
    }
    std::vector<ll> deep(n),des(n);
    dfs(root,0,deep,des,child);
    ll ans = 0;
    for(int i=0;i<n-1;i++){
        ll a=AB.at(i).first,b=AB.at(i).second;
        ans += (deep.at(a)+1)*(des.at(b)+1)%mod;
        ans %= mod;
    }
    std::cout << ans << std::endl;
}
            
            
            
        