結果

問題 No.1103 Directed Length Sum
ユーザー milanis48663220milanis48663220
提出日時 2020-07-03 21:50:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 819 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 85,720 KB
実行使用メモリ 110,084 KB
最終ジャッジ日時 2023-10-17 00:39:15
合計ジャッジ時間 7,190 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
28,216 KB
testcase_01 AC 6 ms
28,216 KB
testcase_02 WA -
testcase_03 AC 238 ms
66,820 KB
testcase_04 AC 316 ms
49,028 KB
testcase_05 AC 679 ms
63,376 KB
testcase_06 AC 185 ms
42,464 KB
testcase_07 AC 41 ms
31,156 KB
testcase_08 AC 60 ms
32,704 KB
testcase_09 AC 28 ms
30,068 KB
testcase_10 AC 85 ms
34,360 KB
testcase_11 AC 363 ms
50,724 KB
testcase_12 AC 177 ms
42,588 KB
testcase_13 AC 87 ms
34,544 KB
testcase_14 AC 22 ms
29,612 KB
testcase_15 AC 135 ms
39,868 KB
testcase_16 AC 381 ms
53,256 KB
testcase_17 AC 405 ms
54,224 KB
testcase_18 AC 81 ms
34,328 KB
testcase_19 AC 333 ms
51,236 KB
testcase_20 AC 32 ms
30,456 KB
testcase_21 AC 52 ms
32,280 KB
testcase_22 AC 255 ms
47,396 KB
testcase_23 AC 146 ms
40,544 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,int 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