結果

問題 No.1103 Directed Length Sum
ユーザー DriceDrice
提出日時 2020-07-03 22:28:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 663 bytes
コンパイル時間 361 ms
コンパイル使用メモリ 48,820 KB
実行使用メモリ 147,700 KB
最終ジャッジ日時 2023-10-17 05:26:08
合計ジャッジ時間 7,477 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
29,468 KB
testcase_01 AC 12 ms
29,468 KB
testcase_02 WA -
testcase_03 AC 191 ms
41,956 KB
testcase_04 AC 359 ms
45,380 KB
testcase_05 AC 674 ms
54,096 KB
testcase_06 AC 227 ms
42,204 KB
testcase_07 AC 46 ms
33,608 KB
testcase_08 AC 71 ms
34,360 KB
testcase_09 AC 32 ms
30,800 KB
testcase_10 AC 104 ms
35,168 KB
testcase_11 AC 400 ms
48,264 KB
testcase_12 AC 229 ms
42,268 KB
testcase_13 AC 111 ms
35,252 KB
testcase_14 AC 26 ms
30,468 KB
testcase_15 AC 173 ms
38,900 KB
testcase_16 AC 467 ms
49,480 KB
testcase_17 AC 490 ms
49,976 KB
testcase_18 AC 106 ms
35,148 KB
testcase_19 AC 412 ms
48,508 KB
testcase_20 AC 37 ms
31,080 KB
testcase_21 AC 64 ms
34,156 KB
testcase_22 AC 338 ms
44,588 KB
testcase_23 AC 187 ms
39,208 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];
        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