結果

問題 No.1103 Directed Length Sum
ユーザー hedwig100hedwig100
提出日時 2020-07-03 23:12:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,072 ms / 3,000 ms
コード長 998 bytes
コンパイル時間 7,935 ms
コンパイル使用メモリ 172,428 KB
実行使用メモリ 112,592 KB
最終ジャッジ日時 2023-10-17 06:14:10
合計ジャッジ時間 12,433 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 746 ms
112,592 KB
testcase_03 AC 492 ms
38,744 KB
testcase_04 AC 601 ms
29,924 KB
testcase_05 AC 1,072 ms
49,496 KB
testcase_06 AC 370 ms
20,720 KB
testcase_07 AC 73 ms
7,520 KB
testcase_08 AC 121 ms
9,632 KB
testcase_09 AC 45 ms
5,936 KB
testcase_10 AC 173 ms
11,980 KB
testcase_11 AC 655 ms
32,336 KB
testcase_12 AC 374 ms
20,820 KB
testcase_13 AC 183 ms
12,204 KB
testcase_14 AC 33 ms
5,408 KB
testcase_15 AC 288 ms
16,888 KB
testcase_16 AC 751 ms
36,032 KB
testcase_17 AC 796 ms
37,352 KB
testcase_18 AC 170 ms
11,940 KB
testcase_19 AC 679 ms
33,248 KB
testcase_20 AC 60 ms
6,464 KB
testcase_21 AC 108 ms
9,104 KB
testcase_22 AC 551 ms
27,772 KB
testcase_23 AC 313 ms
17,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:48:14: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
   48 |     dist[root] = 0;
      |              ^
main.cpp:41:9: note: 'root' was declared here
   41 |     int root;
      |         ^~~~

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int N;
vector<vector<int>> G;
ll ans = 0;
vector<int> dist(N);

int dfs(int v) {
    int cnt = 1;
    for (int e:G[v]) {
        dist[e] = dist[v] + 1;
        cnt += dfs(e);
    }
    ans += (ll)cnt*dist[v];
    ans %= MOD;
    return cnt;
}

int main(){
    cin >> N;
    G.resize(N);
    dist.resize(N);
    fill(dist.begin(),dist.end(),-1);
    vector<int> deg(N,0);

    rep(i,N - 1) {
        int a,b; cin >> a >> b;
        --a; --b;
        G[a].push_back(b);
        deg[b] ++;
    }
    int root;
    rep(i,N) {
        if (deg[i] == 0) {
            root = i;
            break;
        }
    }
    dist[root] = 0;
    dfs(root);
    cout << ans << endl;
    return 0;
}
0