結果

問題 No.1103 Directed Length Sum
ユーザー betrue12
提出日時 2020-07-03 21:46:15
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 979 ms / 3,000 ms
コード長 802 bytes
コンパイル時間 2,077 ms
コンパイル使用メモリ 198,232 KB
最終ジャッジ日時 2025-01-11 14:25:03
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int64_t MOD = 1e9+7;
void add(int64_t& a, int64_t b){
    a = (a+b) % MOD;
}
void mul(int64_t& a, int64_t b){
    a = a*b % MOD;
}

int main(){
    int N;
    cin >> N;
    static vector<int> edges[1000000];
    vector<bool> in(N);
    for(int i=0; i<N-1; i++){
        int a, b;
        cin >> a >> b;
        edges[a-1].push_back(b-1);
        in[b-1] = true;
    }
    int R = -1;
    for(int i=0; i<N; i++) if(!in[i]) R = i;

    int64_t ans = 0;
    auto dfs = [&](auto&& dfs, int i, int64_t d)->int{
        int res = 1;
        for(int j : edges[i]){
            int low = dfs(dfs, j, d+1);
            add(ans, d*low);
            res += low;
        }
        return res;
    };
    dfs(dfs, R, 1);
    cout << ans << endl;
    return 0;
}
0