結果

問題 No.1103 Directed Length Sum
ユーザー betrue12betrue12
提出日時 2020-07-03 21:46:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 883 ms / 3,000 ms
コード長 802 bytes
コンパイル時間 2,226 ms
コンパイル使用メモリ 210,040 KB
実行使用メモリ 91,468 KB
最終ジャッジ日時 2023-10-17 00:24:47
合計ジャッジ時間 15,172 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
27,116 KB
testcase_01 AC 9 ms
27,116 KB
testcase_02 AC 721 ms
91,468 KB
testcase_03 AC 494 ms
31,808 KB
testcase_04 AC 490 ms
36,284 KB
testcase_05 AC 883 ms
43,004 KB
testcase_06 AC 308 ms
33,084 KB
testcase_07 AC 70 ms
28,552 KB
testcase_08 AC 118 ms
29,312 KB
testcase_09 AC 47 ms
28,024 KB
testcase_10 AC 147 ms
30,124 KB
testcase_11 AC 537 ms
37,124 KB
testcase_12 AC 310 ms
33,148 KB
testcase_13 AC 151 ms
30,204 KB
testcase_14 AC 37 ms
27,796 KB
testcase_15 AC 241 ms
31,820 KB
testcase_16 AC 606 ms
38,352 KB
testcase_17 AC 633 ms
38,852 KB
testcase_18 AC 144 ms
30,100 KB
testcase_19 AC 554 ms
37,372 KB
testcase_20 AC 57 ms
28,216 KB
testcase_21 AC 99 ms
29,104 KB
testcase_22 AC 442 ms
35,488 KB
testcase_23 AC 255 ms
32,132 KB
権限があれば一括ダウンロードができます

ソースコード

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