結果

問題 No.1103 Directed Length Sum
ユーザー milanis48663220milanis48663220
提出日時 2020-07-03 21:55:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 786 ms / 3,000 ms
コード長 849 bytes
コンパイル時間 748 ms
コンパイル使用メモリ 85,604 KB
実行使用メモリ 110,048 KB
最終ジャッジ日時 2023-10-17 02:04:02
合計ジャッジ時間 8,380 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
28,180 KB
testcase_01 AC 8 ms
28,180 KB
testcase_02 AC 349 ms
110,048 KB
testcase_03 AC 239 ms
66,784 KB
testcase_04 AC 402 ms
48,992 KB
testcase_05 AC 786 ms
63,340 KB
testcase_06 AC 229 ms
42,428 KB
testcase_07 AC 44 ms
31,120 KB
testcase_08 AC 68 ms
32,668 KB
testcase_09 AC 30 ms
30,032 KB
testcase_10 AC 93 ms
34,324 KB
testcase_11 AC 446 ms
50,688 KB
testcase_12 AC 247 ms
42,552 KB
testcase_13 AC 110 ms
34,508 KB
testcase_14 AC 26 ms
29,576 KB
testcase_15 AC 182 ms
39,832 KB
testcase_16 AC 512 ms
53,220 KB
testcase_17 AC 556 ms
54,188 KB
testcase_18 AC 100 ms
34,292 KB
testcase_19 AC 482 ms
51,200 KB
testcase_20 AC 36 ms
30,420 KB
testcase_21 AC 64 ms
32,244 KB
testcase_22 AC 379 ms
47,360 KB
testcase_23 AC 222 ms
40,508 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:44:8: warning: 'r' may be used uninitialized [-Wmaybe-uninitialized]
   44 |     dfs(r, 0);
      |     ~~~^~~~~~
main.cpp:37:9: note: 'r' was declared here
   37 |     int r;
      |         ^

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;
ll N;
int par[1000001];
const ll MOD = 1000000007;

vector<int> G[1000000];
bool used[1000000];
ll ans;
void dfs(int v,ll d){
    used[v] = true;
    ans += (d*(d+1))/2;
    for(int to : G[v]){
        if(!used[to]) dfs(to, d+1);
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N;
    for(int i = 0; i < N-1; i++){
        int a, b; cin >> a >> b;
        par[b] = a;
        a--; b--;
        G[a].push_back(b); G[b].push_back(a);
    }
    int r;
    for(int i = 1; i <= N; i++){
        if(par[i] == 0){
            r = i-1;
            break;
        }
    }
    dfs(r, 0);
    cout << ans%MOD << endl;
}
0