結果

問題 No.1103 Directed Length Sum
ユーザー itohdakitohdak
提出日時 2020-07-03 22:22:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 724 ms / 3,000 ms
コード長 957 bytes
コンパイル時間 2,121 ms
コンパイル使用メモリ 207,448 KB
実行使用メモリ 161,168 KB
最終ジャッジ日時 2023-10-17 04:31:43
合計ジャッジ時間 8,901 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 351 ms
161,168 KB
testcase_03 AC 220 ms
85,352 KB
testcase_04 AC 359 ms
56,296 KB
testcase_05 AC 724 ms
95,020 KB
testcase_06 AC 197 ms
37,628 KB
testcase_07 AC 35 ms
11,636 KB
testcase_08 AC 54 ms
15,792 KB
testcase_09 AC 21 ms
8,468 KB
testcase_10 AC 76 ms
20,476 KB
testcase_11 AC 398 ms
60,980 KB
testcase_12 AC 202 ms
38,156 KB
testcase_13 AC 82 ms
21,004 KB
testcase_14 AC 15 ms
7,148 KB
testcase_15 AC 140 ms
30,372 KB
testcase_16 AC 469 ms
68,236 KB
testcase_17 AC 501 ms
71,140 KB
testcase_18 AC 90 ms
20,476 KB
testcase_19 AC 412 ms
62,564 KB
testcase_20 AC 27 ms
9,524 KB
testcase_21 AC 49 ms
14,736 KB
testcase_22 AC 327 ms
51,612 KB
testcase_23 AC 159 ms
32,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<vector<int>> G(n);
  vector<vector<int>> from(n);
  rep(i, n-1) {
    int a, b;
    cin >> a >> b; a--; b--;
    G[a].push_back(b);
    from[b].push_back(a);
  }
  int root = 0;
  rep(i, n) if(from[i].size() == 0) root = i;
  ll ans = 0;
  ll tmp = 0;
  auto dfs = [&](auto dfs, int cur, int par, ll depth) -> void {
    ll in = tmp;
    tmp++;
    for(int ne: G[cur]) dfs(dfs, ne, cur, depth+1);
    ll out = tmp;
    (ans += (out-in) * depth) %= mod;
  };
  dfs(dfs, root, -1, 0);
  cout << ans << "\n";
  return 0;
}
0