結果

問題 No.1103 Directed Length Sum
ユーザー bonKotsubonKotsu
提出日時 2021-05-31 14:03:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,171 ms / 3,000 ms
コード長 869 bytes
コンパイル時間 1,989 ms
コンパイル使用メモリ 171,480 KB
実行使用メモリ 112,652 KB
最終ジャッジ日時 2024-04-26 08:04:55
合計ジャッジ時間 14,350 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
28,356 KB
testcase_01 AC 10 ms
27,080 KB
testcase_02 AC 738 ms
112,652 KB
testcase_03 AC 502 ms
38,680 KB
testcase_04 AC 644 ms
41,984 KB
testcase_05 AC 1,171 ms
50,200 KB
testcase_06 AC 381 ms
38,012 KB
testcase_07 AC 81 ms
30,136 KB
testcase_08 AC 125 ms
30,532 KB
testcase_09 AC 52 ms
28,464 KB
testcase_10 AC 172 ms
31,940 KB
testcase_11 AC 714 ms
43,056 KB
testcase_12 AC 385 ms
37,892 KB
testcase_13 AC 179 ms
31,940 KB
testcase_14 AC 42 ms
28,868 KB
testcase_15 AC 307 ms
36,344 KB
testcase_16 AC 798 ms
44,584 KB
testcase_17 AC 836 ms
45,124 KB
testcase_18 AC 173 ms
31,680 KB
testcase_19 AC 723 ms
43,232 KB
testcase_20 AC 62 ms
29,244 KB
testcase_21 AC 111 ms
29,764 KB
testcase_22 AC 579 ms
40,948 KB
testcase_23 AC 323 ms
34,368 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:50:10: warning: 'r' may be used uninitialized [-Wmaybe-uninitialized]
   50 |   dfs_sum(r,0);
      |   ~~~~~~~^~~~~
main.cpp:41:7: note: 'r' was declared here
   41 |   int r;
      |       ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>

const int MOD = 1e9+7;

vector<int> G[1000005];
int sum[1000005];
vector<int> dist;

void dfs(int v, int d) {
  dist[v] = d;
  for (int nv : G[v]) {
    dfs(nv, d+1);
  }
}

void dfs_sum(int v, int d) {
  sum[v] = d + dist[v];
  sum[v] %= MOD;
  for (int nv : G[v]) {
    dfs_sum(nv, sum[v]);
  }
}

int main() {
  int n;
  cin >> n;
  vector<bool> seen(n, true);
  rep(i,n-1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    seen[b] = false;
  }

  int r;
  // 根を決定
  rep(i,n) {
    if (seen[i]) r = i;
  }

  const int INF = 1001001001;
  dist.resize(n, INF);
  dfs(r, 0);
  dfs_sum(r,0);

  int ans = 0;
  rep(i,n) {
    ans += sum[i];
    ans %= MOD;
  }
  cout << ans << endl;
  






}
0