結果

問題 No.1103 Directed Length Sum
ユーザー milanis48663220milanis48663220
提出日時 2020-07-03 21:50:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 818 bytes
コンパイル時間 6,405 ms
コンパイル使用メモリ 85,420 KB
実行使用メモリ 110,072 KB
最終ジャッジ日時 2023-10-17 00:55:18
合計ジャッジ時間 10,767 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
28,204 KB
testcase_01 AC 9 ms
28,204 KB
testcase_02 WA -
testcase_03 AC 241 ms
66,816 KB
testcase_04 AC 401 ms
49,016 KB
testcase_05 AC 722 ms
63,364 KB
testcase_06 AC 260 ms
42,452 KB
testcase_07 AC 61 ms
31,144 KB
testcase_08 AC 83 ms
32,692 KB
testcase_09 AC 33 ms
30,056 KB
testcase_10 AC 114 ms
34,348 KB
testcase_11 AC 445 ms
50,712 KB
testcase_12 AC 261 ms
42,576 KB
testcase_13 AC 122 ms
34,532 KB
testcase_14 AC 26 ms
29,600 KB
testcase_15 AC 191 ms
39,856 KB
testcase_16 AC 499 ms
53,244 KB
testcase_17 AC 518 ms
54,212 KB
testcase_18 AC 112 ms
34,316 KB
testcase_19 AC 458 ms
51,224 KB
testcase_20 AC 38 ms
30,444 KB
testcase_21 AC 70 ms
32,268 KB
testcase_22 AC 365 ms
47,384 KB
testcase_23 AC 209 ms
40,532 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:43:8: warning: 'r' may be used uninitialized [-Wmaybe-uninitialized]
   43 |     dfs(r, 0);
      |     ~~~^~~~~~
main.cpp:36:9: note: 'r' was declared here
   36 |     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];

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 << endl;
}
0