結果

問題 No.1103 Directed Length Sum
ユーザー siman
提出日時 2021-10-12 02:48:51
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 790 bytes
コンパイル時間 1,263 ms
コンパイル使用メモリ 141,308 KB
実行使用メモリ 108,928 KB
最終ジャッジ日時 2024-09-16 10:13:35
合計ジャッジ時間 13,361 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21 WA * 1
権限があれば一括ダウンロードができます

ソースコード

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