結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 274 ms
69,576 KB
testcase_04 AC 534 ms
38,948 KB
testcase_05 AC 985 ms
65,132 KB
testcase_06 AC 316 ms
26,424 KB
testcase_07 AC 54 ms
8,640 KB
testcase_08 AC 93 ms
11,820 KB
testcase_09 AC 30 ms
6,492 KB
testcase_10 AC 145 ms
14,900 KB
testcase_11 AC 583 ms
42,164 KB
testcase_12 AC 326 ms
26,576 KB
testcase_13 AC 144 ms
15,116 KB
testcase_14 AC 20 ms
5,996 KB
testcase_15 AC 243 ms
21,540 KB
testcase_16 AC 656 ms
47,168 KB
testcase_17 AC 690 ms
49,052 KB
testcase_18 AC 139 ms
14,968 KB
testcase_19 AC 600 ms
43,480 KB
testcase_20 AC 35 ms
7,324 KB
testcase_21 AC 83 ms
10,644 KB
testcase_22 AC 476 ms
35,780 KB
testcase_23 AC 262 ms
22,728 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 dfs(int cur, int pre) {
    child[cur] = 1;
    for(auto next : g[cur]) {
        if(next == pre) continue;
        depth[next] = depth[cur] + 1;
        dfs(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; 

    // 根から探索
    dfs(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