結果

問題 No.1103 Directed Length Sum
ユーザー iqueue02iqueue02
提出日時 2020-07-03 22:18:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,100 ms / 3,000 ms
コード長 1,874 bytes
コンパイル時間 2,248 ms
コンパイル使用メモリ 209,584 KB
実行使用メモリ 121,760 KB
最終ジャッジ日時 2023-10-17 04:25:03
合計ジャッジ時間 13,056 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 738 ms
121,760 KB
testcase_03 AC 498 ms
50,220 KB
testcase_04 AC 610 ms
36,580 KB
testcase_05 AC 1,100 ms
60,844 KB
testcase_06 AC 383 ms
24,964 KB
testcase_07 AC 75 ms
8,604 KB
testcase_08 AC 123 ms
11,176 KB
testcase_09 AC 46 ms
6,492 KB
testcase_10 AC 180 ms
14,344 KB
testcase_11 AC 674 ms
39,672 KB
testcase_12 AC 388 ms
25,228 KB
testcase_13 AC 187 ms
14,608 KB
testcase_14 AC 33 ms
5,700 KB
testcase_15 AC 295 ms
20,280 KB
testcase_16 AC 759 ms
44,092 KB
testcase_17 AC 799 ms
45,872 KB
testcase_18 AC 178 ms
14,080 KB
testcase_19 AC 683 ms
40,464 KB
testcase_20 AC 56 ms
7,284 KB
testcase_21 AC 112 ms
10,452 KB
testcase_22 AC 552 ms
33,736 KB
testcase_23 AC 319 ms
21,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll, int> PL;
constexpr int mod = 1e9+7;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

int main() {
  int n;
  cin >> n;
  vector<int> deg(n, 0);
  vector<vector<int>> g(n);
  rep(i,n - 1) {
    int a, b;
    cin >> a >> b;
    a--; b--;
    g[a].emplace_back(b);
    deg[b]++;
  }

  int root = -1;

  for (int i = 0; i < n; i++) {
    if (!deg[i]) root = i;
  }

  vector<mint> dp1(n, 0), dp2(n, 0);
  mint res = 0;

  auto dfs = [&](auto& f, int u)->void{
    dp1[u] += 1;
    for (auto v : g[u]) {
      dp1[v] += dp1[u];
      f(f, v);
      dp2[u] += dp2[v];
    }
    res += dp1[u] * dp2[u];
    dp2[u] += 1;
  };

  dfs(dfs, root);

  cout << res << endl;
  return 0;
} 
0