結果

問題 No.1103 Directed Length Sum
ユーザー roarisroaris
提出日時 2020-07-03 22:00:28
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 707 ms / 3,000 ms
コード長 952 bytes
コンパイル時間 2,329 ms
コンパイル使用メモリ 207,896 KB
実行使用メモリ 135,128 KB
最終ジャッジ日時 2023-10-17 02:14:53
合計ジャッジ時間 9,520 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
31,376 KB
testcase_01 AC 11 ms
31,376 KB
testcase_02 AC 310 ms
135,128 KB
testcase_03 AC 181 ms
73,696 KB
testcase_04 AC 375 ms
65,204 KB
testcase_05 AC 707 ms
83,536 KB
testcase_06 AC 247 ms
53,440 KB
testcase_07 AC 52 ms
36,816 KB
testcase_08 AC 80 ms
40,796 KB
testcase_09 AC 33 ms
33,376 KB
testcase_10 AC 115 ms
44,104 KB
testcase_11 AC 443 ms
68,276 KB
testcase_12 AC 246 ms
53,456 KB
testcase_13 AC 122 ms
44,384 KB
testcase_14 AC 28 ms
32,800 KB
testcase_15 AC 198 ms
47,712 KB
testcase_16 AC 485 ms
71,684 KB
testcase_17 AC 506 ms
76,428 KB
testcase_18 AC 114 ms
44,100 KB
testcase_19 AC 445 ms
68,596 KB
testcase_20 AC 40 ms
33,892 KB
testcase_21 AC 70 ms
37,988 KB
testcase_22 AC 352 ms
61,684 KB
testcase_23 AC 204 ms
50,364 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:38:8: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
   38 |     dfs(root, -1, 0);
      |     ~~~^~~~~~~~~~~~~
main.cpp:36:9: note: 'root' was declared here
   36 |     int root;
      |         ^~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define int long long
const int MOD = 1000000007;
typedef pair<int, int> P;

int N;
vector<int> G[1000010];
vector<P> edges;
int deg[1000010];
int depth[1000010];
int ch[1000010];

int dfs(int v, int pv, int dep) {
    depth[v] = dep;
    int res = 1;
    for (auto& nv:G[v]) {
        if (nv==pv) continue;
        res += dfs(nv, v, dep+1);
    }
    return ch[v] = res;
}
 
signed main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N;
    rep(i, N-1) {
        int A, B; cin >> A >> B;
        G[A-1].pb(B-1);
        deg[B-1]++;
        edges.pb(P(A-1, B-1));
    }
    
    int root;
    rep(i, N) if (deg[i]==0) root = i;
    dfs(root, -1, 0);
    
    int ans = 0;
    rep(i, N-1) {
        int A = edges[i].first, B = edges[i].second;
        ans += ch[B]*(depth[A]+1);
        ans %= MOD;
    }
    cout << ans << endl;
}
0