結果

問題 No.1103 Directed Length Sum
ユーザー onsen_manjuuuonsen_manjuuu
提出日時 2020-07-05 16:19:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,065 ms / 3,000 ms
コード長 816 bytes
コンパイル時間 2,803 ms
コンパイル使用メモリ 174,904 KB
実行使用メモリ 136,060 KB
最終ジャッジ日時 2023-10-23 20:05:20
合計ジャッジ時間 12,454 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 747 ms
136,060 KB
testcase_03 AC 489 ms
51,336 KB
testcase_04 AC 573 ms
35,756 KB
testcase_05 AC 1,065 ms
59,568 KB
testcase_06 AC 358 ms
24,412 KB
testcase_07 AC 68 ms
8,316 KB
testcase_08 AC 109 ms
10,956 KB
testcase_09 AC 42 ms
6,468 KB
testcase_10 AC 153 ms
14,056 KB
testcase_11 AC 642 ms
38,856 KB
testcase_12 AC 359 ms
24,676 KB
testcase_13 AC 165 ms
14,320 KB
testcase_14 AC 31 ms
5,676 KB
testcase_15 AC 268 ms
20,060 KB
testcase_16 AC 724 ms
43,276 KB
testcase_17 AC 763 ms
44,792 KB
testcase_18 AC 156 ms
13,792 KB
testcase_19 AC 657 ms
39,648 KB
testcase_20 AC 50 ms
7,260 KB
testcase_21 AC 96 ms
10,428 KB
testcase_22 AC 520 ms
32,920 KB
testcase_23 AC 285 ms
21,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;


ll n, root;
vector<vector<ll>> hen;
vector<ll> dp;



pair<ll,ll> dfs(ll v, ll par) {
    ll sum = 0;
    ll cnt = 1;

    for(auto i : hen[v]) {
        pair<ll,ll> cur = dfs(i, v);
        sum += (cur.first + cur.second);
        cnt += cur.second;
        sum %= mod;
    }
    dp[v] = sum;
    return {sum, cnt};
}



int main()
{
    cin >> n;
    hen.resize(n), dp.resize(n);
    vector<ll> fin(n);

    for(ll i = 0; i < n - 1; i++) {
        ll a, b; cin >> a >> b; a--, b--;
        hen[a].emplace_back(b);
        fin[b]++;
    }
    root = find(fin.begin(), fin.end(), 0) - fin.begin();


    
    dfs(root, -1);
    cout << accumulate(dp.begin(), dp.end(),0LL, [](ll a, ll b){return (a + b) % mod;}) << endl;
}
0