結果

問題 No.1103 Directed Length Sum
ユーザー DriceDrice
提出日時 2020-07-03 22:34:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 658 ms / 3,000 ms
コード長 667 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 48,908 KB
実行使用メモリ 147,772 KB
最終ジャッジ日時 2023-10-17 05:34:19
合計ジャッジ時間 7,257 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
29,540 KB
testcase_01 AC 12 ms
29,540 KB
testcase_02 AC 355 ms
147,772 KB
testcase_03 AC 191 ms
42,036 KB
testcase_04 AC 370 ms
45,452 KB
testcase_05 AC 658 ms
54,168 KB
testcase_06 AC 225 ms
42,276 KB
testcase_07 AC 46 ms
33,680 KB
testcase_08 AC 69 ms
34,432 KB
testcase_09 AC 31 ms
30,872 KB
testcase_10 AC 106 ms
35,240 KB
testcase_11 AC 398 ms
48,336 KB
testcase_12 AC 227 ms
42,340 KB
testcase_13 AC 109 ms
35,324 KB
testcase_14 AC 27 ms
30,540 KB
testcase_15 AC 177 ms
38,972 KB
testcase_16 AC 451 ms
49,552 KB
testcase_17 AC 473 ms
50,048 KB
testcase_18 AC 107 ms
35,220 KB
testcase_19 AC 407 ms
48,580 KB
testcase_20 AC 37 ms
31,152 KB
testcase_21 AC 63 ms
34,228 KB
testcase_22 AC 325 ms
44,660 KB
testcase_23 AC 191 ms
39,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<vector>
std::vector<int>g[1000005];
int in[1000005];
long long son[1000005];
const long long mod = 1e9+7;
long long ans = 0;

void dfs(long long d,int u){
    son[u] = 1;
    for(int i = 0; i < (int)g[u].size(); i++){
        int v = g[u][i];
        dfs(d+1,v);
        son[u] += son[v];
        ans += d*son[v]%mod;
        if(ans>=mod) ans -= mod;
    }
}

int main(){
    int n;
    scanf("%d",&n);
    for(int i = 0; i < n-1; i++){
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        in[v]++;
    }
    ans = 0;
    for(int i = 1; i <= n; i++) if(!in[i]) dfs(1,i);
    printf("%lld\n",ans);
    return 0;
}
0