結果

問題 No.1103 Directed Length Sum
ユーザー iqueue02iqueue02
提出日時 2020-07-03 22:22:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 671 ms / 3,000 ms
コード長 1,925 bytes
コンパイル時間 2,303 ms
コンパイル使用メモリ 209,564 KB
実行使用メモリ 121,628 KB
最終ジャッジ日時 2023-10-17 04:31:33
合計ジャッジ時間 8,925 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 315 ms
121,628 KB
testcase_03 AC 186 ms
50,104 KB
testcase_04 AC 362 ms
36,448 KB
testcase_05 AC 671 ms
60,976 KB
testcase_06 AC 224 ms
24,832 KB
testcase_07 AC 39 ms
8,472 KB
testcase_08 AC 66 ms
11,308 KB
testcase_09 AC 23 ms
6,624 KB
testcase_10 AC 101 ms
14,212 KB
testcase_11 AC 400 ms
39,540 KB
testcase_12 AC 229 ms
25,096 KB
testcase_13 AC 107 ms
14,476 KB
testcase_14 AC 17 ms
5,568 KB
testcase_15 AC 168 ms
20,148 KB
testcase_16 AC 455 ms
43,960 KB
testcase_17 AC 480 ms
45,740 KB
testcase_18 AC 106 ms
13,948 KB
testcase_19 AC 422 ms
40,332 KB
testcase_20 AC 28 ms
7,152 KB
testcase_21 AC 60 ms
10,320 KB
testcase_22 AC 330 ms
33,604 KB
testcase_23 AC 190 ms
21,468 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() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  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