結果

問題 No.1103 Directed Length Sum
ユーザー Asdf_QwertyZAsdf_QwertyZ
提出日時 2020-07-05 20:53:52
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,120 ms / 3,000 ms
コード長 3,207 bytes
コンパイル時間 1,208 ms
コンパイル使用メモリ 82,868 KB
実行使用メモリ 144,272 KB
最終ジャッジ日時 2023-10-24 04:26:37
合計ジャッジ時間 12,199 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
43,712 KB
testcase_01 AC 15 ms
43,712 KB
testcase_02 AC 754 ms
144,272 KB
testcase_03 AC 496 ms
58,188 KB
testcase_04 AC 630 ms
58,984 KB
testcase_05 AC 1,120 ms
68,728 KB
testcase_06 AC 395 ms
55,316 KB
testcase_07 AC 91 ms
48,072 KB
testcase_08 AC 139 ms
48,944 KB
testcase_09 AC 60 ms
45,184 KB
testcase_10 AC 195 ms
49,876 KB
testcase_11 AC 691 ms
61,996 KB
testcase_12 AC 402 ms
55,388 KB
testcase_13 AC 200 ms
49,972 KB
testcase_14 AC 49 ms
44,816 KB
testcase_15 AC 312 ms
51,816 KB
testcase_16 AC 782 ms
63,408 KB
testcase_17 AC 810 ms
63,972 KB
testcase_18 AC 191 ms
49,848 KB
testcase_19 AC 707 ms
62,276 KB
testcase_20 AC 74 ms
45,492 KB
testcase_21 AC 126 ms
48,704 KB
testcase_22 AC 569 ms
58,072 KB
testcase_23 AC 332 ms
52,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define repdi(i,a,b) for(ll i=(a)-1;i>=(b);--i)
#define repd(i,a) repdi(i,a,0)
#define itr(it,a) for( auto it = (a).begin(); it != (a).end(); ++it )
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll<<60;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class S, class T>
std::ostream& operator<< ( std::ostream& out, const std::pair<S,T>& a )
{ std::cout << '(' << a.first << ", " << a.second << ')'; return out; }

template<class T>
std::ostream &operator<< ( std::ostream& out, const std::vector<T>& a )
{ std::cout << '['; rep( i, a.size() ){ std::cout << a[i]; if( i != a.size()-1 ) std::cout << ", "; } std::cout << ']'; return out; }

const ll mod = 1000000007;

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 {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=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 {
    mint res(*this);
    return res/=a;
  }
};

struct combination {
  std::vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
};

ll N;
std::vector<ll> G[1000010];
mint dp[1000010];
mint subtree[1000010];

void dfs( ll v, ll p = -1 ) {
  subtree[v] = 1;

  for( auto u : G[v] ) if( u != p ) {
    dfs( u, v );

    subtree[v] += subtree[u];
    dp[v] += dp[u]+subtree[u];
  }

  return;
}

ll deg[1000010];

int main()
{
  std::cin >> N;

  rep( i, N-1 ) {
    ll A, B;
    std::cin >> A >> B;
    --A; --B;

    G[A].emplace_back( B );
    ++deg[B];
  }

  rep( i, N ) if( !deg[i] )
    dfs( i );

  mint ans = 0;

  rep( i, N ) {
    ans += dp[i];
  }

  std::cout << ans.x << std::endl;

  return 0;
}
0