結果

問題 No.1103 Directed Length Sum
ユーザー simansiman
提出日時 2021-10-12 02:48:51
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 790 bytes
コンパイル時間 971 ms
コンパイル使用メモリ 103,800 KB
実行使用メモリ 108,912 KB
最終ジャッジ日時 2023-10-14 15:47:09
合計ジャッジ時間 14,088 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
30,752 KB
testcase_01 AC 11 ms
30,760 KB
testcase_02 WA -
testcase_03 AC 475 ms
36,108 KB
testcase_04 AC 557 ms
39,872 KB
testcase_05 AC 993 ms
46,608 KB
testcase_06 AC 349 ms
36,724 KB
testcase_07 AC 78 ms
32,296 KB
testcase_08 AC 120 ms
33,028 KB
testcase_09 AC 52 ms
31,704 KB
testcase_10 AC 166 ms
33,752 KB
testcase_11 AC 604 ms
40,696 KB
testcase_12 AC 350 ms
36,796 KB
testcase_13 AC 172 ms
33,920 KB
testcase_14 AC 41 ms
31,476 KB
testcase_15 AC 273 ms
35,460 KB
testcase_16 AC 694 ms
41,996 KB
testcase_17 AC 725 ms
42,460 KB
testcase_18 AC 169 ms
33,712 KB
testcase_19 AC 629 ms
41,092 KB
testcase_20 AC 62 ms
31,992 KB
testcase_21 AC 107 ms
32,776 KB
testcase_22 AC 499 ms
39,056 KB
testcase_23 AC 287 ms
35,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_N = 1000010;
vector<int> E[MAX_N];
int P[MAX_N];
int N;

int find_root(int v) {
  if (P[v] == -1) return v;
  return find_root(P[v]);
}

ll dfs(int v, int depth) {
  ll len = 0;

  for (int u : E[v]) {
    len += depth * (depth + 1) / 2;
    len += dfs(u, depth + 1);
  }

  return len;
}

int main() {
  memset(P, -1, sizeof(P));
  cin >> N;

  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;

    E[a].push_back(b);
    P[b] = a;
  }

  int root = find_root(1);
  cout << dfs(root, 1) << endl;

  return 0;
}
0