結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  milanis48663220 | 
| 提出日時 | 2020-07-03 21:55:04 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 907 ms / 3,000 ms | 
| コード長 | 849 bytes | 
| コンパイル時間 | 893 ms | 
| コンパイル使用メモリ | 85,756 KB | 
| 実行使用メモリ | 109,824 KB | 
| 最終ジャッジ日時 | 2024-09-17 00:38:15 | 
| 合計ジャッジ時間 | 9,531 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 22 | 
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:44:8: warning: 'r' may be used uninitialized [-Wmaybe-uninitialized]
   44 |     dfs(r, 0);
      |     ~~~^~~~~~
main.cpp:37:9: note: 'r' was declared here
   37 |     int r;
      |         ^
            
            ソースコード
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
ll N;
int par[1000001];
const ll MOD = 1000000007;
vector<int> G[1000000];
bool used[1000000];
ll ans;
void dfs(int v,ll d){
    used[v] = true;
    ans += (d*(d+1))/2;
    for(int to : G[v]){
        if(!used[to]) dfs(to, d+1);
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N;
    for(int i = 0; i < N-1; i++){
        int a, b; cin >> a >> b;
        par[b] = a;
        a--; b--;
        G[a].push_back(b); G[b].push_back(a);
    }
    int r;
    for(int i = 1; i <= N; i++){
        if(par[i] == 0){
            r = i-1;
            break;
        }
    }
    dfs(r, 0);
    cout << ans%MOD << endl;
}
            
            
            
        