結果

問題 No.1103 Directed Length Sum
ユーザー simansiman
提出日時 2021-10-12 03:00:15
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,219 bytes
コンパイル時間 1,220 ms
コンパイル使用メモリ 106,496 KB
実行使用メモリ 62,084 KB
最終ジャッジ日時 2023-10-14 16:00:45
合計ジャッジ時間 11,094 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
30,800 KB
testcase_01 AC 10 ms
30,772 KB
testcase_02 WA -
testcase_03 AC 440 ms
42,628 KB
testcase_04 AC 456 ms
40,496 KB
testcase_05 AC 879 ms
47,460 KB
testcase_06 AC 272 ms
37,208 KB
testcase_07 AC 67 ms
32,328 KB
testcase_08 AC 99 ms
33,140 KB
testcase_09 AC 46 ms
31,888 KB
testcase_10 AC 137 ms
34,020 KB
testcase_11 AC 481 ms
41,368 KB
testcase_12 AC 281 ms
37,236 KB
testcase_13 AC 144 ms
34,096 KB
testcase_14 AC 36 ms
31,476 KB
testcase_15 AC 220 ms
35,772 KB
testcase_16 AC 562 ms
42,696 KB
testcase_17 AC 605 ms
43,116 KB
testcase_18 AC 131 ms
34,032 KB
testcase_19 AC 515 ms
41,616 KB
testcase_20 AC 50 ms
31,912 KB
testcase_21 AC 91 ms
32,868 KB
testcase_22 AC 415 ms
39,672 KB
testcase_23 AC 228 ms
36,088 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;
typedef pair<int, int> Node;

const ll MOD = 1000000007;
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 %= MOD;
    len += dfs(u, depth + 1);
    len %= MOD;
  }

  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);
  ll ans = 0;
  // cout << dfs(root, 1) << endl;
  queue<Node> que;
  que.push(Node(root, 1));

  while (not que.empty()) {
    Node node = que.front();
    int v = node.first;
    int depth = node.second;
    que.pop();

    for (int u : E[v]) {
      ans += depth * (depth + 1) / 2;
      ans %= MOD;
      que.push(Node(u, depth + 1));
    }
  }

  cout << ans << endl;

  return 0;
}
0