結果

問題 No.1103 Directed Length Sum
ユーザー SSlimeSSlime
提出日時 2020-07-04 01:37:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,061 ms / 3,000 ms
コード長 1,202 bytes
コンパイル時間 782 ms
コンパイル使用メモリ 82,196 KB
実行使用メモリ 175,204 KB
最終ジャッジ日時 2023-10-17 07:23:09
合計ジャッジ時間 11,251 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 778 ms
175,204 KB
testcase_03 AC 491 ms
58,148 KB
testcase_04 AC 584 ms
40,188 KB
testcase_05 AC 1,061 ms
67,296 KB
testcase_06 AC 361 ms
27,328 KB
testcase_07 AC 74 ms
9,120 KB
testcase_08 AC 116 ms
12,288 KB
testcase_09 AC 44 ms
7,008 KB
testcase_10 AC 163 ms
15,388 KB
testcase_11 AC 644 ms
43,552 KB
testcase_12 AC 369 ms
27,592 KB
testcase_13 AC 173 ms
15,916 KB
testcase_14 AC 32 ms
5,952 KB
testcase_15 AC 275 ms
22,184 KB
testcase_16 AC 720 ms
48,696 KB
testcase_17 AC 756 ms
50,544 KB
testcase_18 AC 156 ms
15,388 KB
testcase_19 AC 638 ms
44,608 KB
testcase_20 AC 50 ms
7,800 KB
testcase_21 AC 98 ms
11,232 KB
testcase_22 AC 506 ms
37,088 KB
testcase_23 AC 278 ms
23,700 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:43:8: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
   43 |     dfs(root,0,deep,des,child);
      |     ~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:35:9: note: 'root' was declared here
   35 |     int root;
      |         ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<utility>

using ll = long long;
using Pii = std::pair<int,int>;

const long long mod = 1000000007;

long long dfs(int now,ll dep,std::vector<long long>& deep,std::vector<long long>& des,std::vector<std::vector<long long>>& child){
    deep.at(now) = dep;
    ll res = 1;
    for(int nxt:child.at(now)){
        ll x = 0;
        x = dfs(nxt,dep+1,deep,des,child);
        res += x;
        des.at(now) += x;
    }
    return res;
}

int main(){
    ll n;
    std::cin >> n;
    std::vector child(n,std::vector<ll>());
    std::vector parent(n,true);
    std::vector AB(n-1,Pii());
    for(int i=0; i<n-1; i++){
        ll a,b;
        std::cin >> a >> b;
        AB.at(i) = {a-1,b-1};
        child.at(a-1).push_back(b-1);
        parent.at(b-1) = false;
    }
    int root;
    for(int i=0; i<n; i++){
        if(parent.at(i)){
            root = i;
            break;
        }
    }
    std::vector<ll> deep(n),des(n);
    dfs(root,0,deep,des,child);
    ll ans = 0;
    for(int i=0;i<n-1;i++){
        ll a=AB.at(i).first,b=AB.at(i).second;
        ans += (deep.at(a)+1)*(des.at(b)+1)%mod;
        ans %= mod;
    }
    std::cout << ans << std::endl;
}
0