結果

問題 No.1103 Directed Length Sum
ユーザー kurotemkokurotemko
提出日時 2020-07-26 21:10:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,285 bytes
コンパイル時間 2,868 ms
コンパイル使用メモリ 205,908 KB
実行使用メモリ 143,896 KB
最終ジャッジ日時 2023-09-11 05:00:47
合計ジャッジ時間 10,359 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 197 ms
39,320 KB
testcase_04 AC 413 ms
30,144 KB
testcase_05 AC 753 ms
49,608 KB
testcase_06 AC 256 ms
20,464 KB
testcase_07 AC 42 ms
7,412 KB
testcase_08 AC 74 ms
9,720 KB
testcase_09 AC 24 ms
5,760 KB
testcase_10 AC 115 ms
11,684 KB
testcase_11 AC 473 ms
32,392 KB
testcase_12 AC 264 ms
20,636 KB
testcase_13 AC 119 ms
12,128 KB
testcase_14 AC 18 ms
5,300 KB
testcase_15 AC 198 ms
16,700 KB
testcase_16 AC 523 ms
35,896 KB
testcase_17 AC 550 ms
37,360 KB
testcase_18 AC 103 ms
11,828 KB
testcase_19 AC 472 ms
33,004 KB
testcase_20 AC 31 ms
6,560 KB
testcase_21 AC 65 ms
8,856 KB
testcase_22 AC 374 ms
27,684 KB
testcase_23 AC 210 ms
17,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;

vector<vector<int>> g;
vector<int> depth;
vector<int> child;

void dfs1(int cur, int pre) {
    for(auto next : g[cur]) {
        if(next == pre) continue;
        depth[next] = depth[cur] + 1;
        dfs1(next, cur);
    }
    return;
}

void dfs2(int cur, int pre) {
    child[cur] = 1;
    for(auto next : g[cur]) {
        if(next == pre) continue;
        dfs2(next, cur);
        child[cur] += child[next];
    }
    return;
}

int main() {

    int n;
    cin >> n;

    // nの長さを確保
    g.resize(n);
    depth.resize(n);
    child.resize(n);

    // 根を探す用の配列
    vector<bool> s_root(n, true);

    // グラフ作成
    for(int i = 0; i < n-1; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--; b--;
        g[a].emplace_back(b);
        //g[b].emplace_back(a);
        s_root[b] = false;
    }

    // 根を探す
    int root = -1;
    for(int i = 0; i < n; ++i) if(s_root[i] == true) root = i; 

    // 根から探索
    dfs1(root, -1);
    dfs2(root, -1);

    // 和の計算
    long long ans = 0;
    for(int i = 0; i < n; ++i) {
        ans += (depth[i]*child[i]) % mod;
        ans %= mod;
    }

    cout << ans << endl;
    return 0;
}
0