結果

問題 No.1103 Directed Length Sum
ユーザー HaarHaar
提出日時 2020-07-04 06:33:25
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,160 ms / 3,000 ms
コード長 5,397 bytes
コンパイル時間 2,951 ms
コンパイル使用メモリ 212,792 KB
実行使用メモリ 126,652 KB
最終ジャッジ日時 2023-10-17 12:11:46
合計ジャッジ時間 15,225 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 765 ms
126,652 KB
testcase_03 AC 500 ms
54,000 KB
testcase_04 AC 632 ms
37,176 KB
testcase_05 AC 1,160 ms
62,040 KB
testcase_06 AC 394 ms
25,300 KB
testcase_07 AC 77 ms
8,608 KB
testcase_08 AC 128 ms
11,512 KB
testcase_09 AC 46 ms
6,760 KB
testcase_10 AC 183 ms
14,348 KB
testcase_11 AC 698 ms
40,272 KB
testcase_12 AC 397 ms
25,564 KB
testcase_13 AC 186 ms
14,876 KB
testcase_14 AC 34 ms
5,968 KB
testcase_15 AC 302 ms
20,616 KB
testcase_16 AC 794 ms
44,956 KB
testcase_17 AC 833 ms
46,736 KB
testcase_18 AC 178 ms
14,348 KB
testcase_19 AC 711 ms
41,328 KB
testcase_20 AC 56 ms
7,288 KB
testcase_21 AC 113 ms
10,720 KB
testcase_22 AC 572 ms
34,336 KB
testcase_23 AC 327 ms
21,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#ifdef DEBUG
#include <Mylib/Debug/debug.cpp>
#else
#define dump(...)
#endif

/**
 * @title Modint
 * @docs mint.md
 */
template <uint32_t M> class ModInt{
public:
  constexpr static uint32_t MOD = M;
  uint64_t val;
  
  constexpr ModInt(): val(0){}
  constexpr ModInt(int64_t n){
    if(n >= M) val = n % M;
    else if(n < 0) val = n % M + M;
    else val = n;
  }
  
  inline constexpr auto operator+(const ModInt &a) const {return ModInt(val + a.val);}
  inline constexpr auto operator-(const ModInt &a) const {return ModInt(val - a.val);}
  inline constexpr auto operator*(const ModInt &a) const {return ModInt(val * a.val);}
  inline constexpr auto operator/(const ModInt &a) const {return ModInt(val * a.inv().val);}
  
  inline constexpr auto& operator=(const ModInt &a){val = a.val; return *this;}
  inline constexpr auto& operator+=(const ModInt &a){if((val += a.val) >= M) val -= M; return *this;}
  inline constexpr auto& operator-=(const ModInt &a){if(val < a.val) val += M; val -= a.val; return *this;}
  inline constexpr auto& operator*=(const ModInt &a){(val *= a.val) %= M; return *this;}
  inline constexpr auto& operator/=(const ModInt &a){(val *= a.inv().val) %= M; return *this;}
  
  inline constexpr bool operator==(const ModInt &a) const {return val == a.val;}
  inline constexpr bool operator!=(const ModInt &a) const {return val != a.val;}
  
  inline constexpr auto& operator++(){*this += 1; return *this;}
  inline constexpr auto& operator--(){*this -= 1; return *this;}
  
  inline constexpr auto operator++(int){auto t = *this; *this += 1; return t;}
  inline constexpr auto operator--(int){auto t = *this; *this -= 1; return t;}
  
  inline constexpr static ModInt power(int64_t n, int64_t p){
    if(p < 0) return power(n, -p).inv();
    
    int64_t ret = 1, e = n % M;
    for(; p; (e *= e) %= M, p >>= 1) if(p & 1) (ret *= e) %= M;
    return ret;
  }
  
  inline constexpr static ModInt inv(int64_t a){
    int64_t b = M, u = 1, v = 0;
    
    while(b){
      int64_t t = a / b;
      a -= t * b; std::swap(a,b);
      u -= t * v; std::swap(u,v);
    }
    
    u %= M;
    if(u < 0) u += M;
    
    return u;
  }
  
  inline constexpr static auto frac(int64_t a, int64_t b){return ModInt(a) / ModInt(b);}
  
  inline constexpr auto power(int64_t p) const {return power(val, p);}
  inline constexpr auto inv() const {return inv(val);}
  
  friend inline constexpr auto operator-(const ModInt &a){return ModInt(-a.val);}
  
  friend inline constexpr auto operator+(int64_t a, const ModInt &b){return ModInt(a) + b;}
  friend inline constexpr auto operator-(int64_t a, const ModInt &b){return ModInt(a) - b;}
  friend inline constexpr auto operator*(int64_t a, const ModInt &b){return ModInt(a) * b;}
  friend inline constexpr auto operator/(int64_t a, const ModInt &b){return ModInt(a) / b;}
  
  friend std::istream& operator>>(std::istream &s, ModInt<M> &a){s >> a.val; return s;}
  friend std::ostream& operator<<(std::ostream &s, const ModInt<M> &a){s << a.val; return s;}

  template <int N>
  inline static auto div(){
    static auto value = inv(N);
    return value;
  }

  explicit operator int32_t() const noexcept {return val;}
  explicit operator int64_t() const noexcept {return val;}
};

using mint = ModInt<1000000007>;

/**
 * @title Graph template
 * @docs graph_template.md
 */
template <typename Cost = int> class Edge{
public:
  int from,to;
  Cost cost;
  Edge() {}
  Edge(int to, Cost cost): to(to), cost(cost){}
  Edge(int from, int to, Cost cost): from(from), to(to), cost(cost){}
};

template <typename T> using Graph = std::vector<std::vector<Edge<T>>>;
template <typename T> using Tree = std::vector<std::vector<Edge<T>>>;

template <typename T, typename C> void add_edge(C &g, int from, int to, T w = 1){
  g[from].emplace_back(from, to, w);
}

template <typename T, typename C> void add_undirected(C &g, int a, int b, T w = 1){
  add_edge<T, C>(g, a, b, w);
  add_edge<T, C>(g, b, a, w);
}

/**
 * @title Fixed point combinator
 * @docs fix_point.md
 */
template <typename F>
struct FixPoint : F{
  explicit constexpr FixPoint(F &&f) noexcept : F(std::forward<F>(f)){}

  template <typename... Args>
  constexpr auto operator()(Args &&... args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};

template <typename F>
inline constexpr auto make_fix_point(F &&f){
  return FixPoint<F>(std::forward<F>(f));
}

template <typename F>
inline constexpr auto make_fix_point(F &f){
  return FixPoint<F>(std::forward<F>(f));
}


int main(){
  int N;

  while(std::cin >> N){
    Tree<int> tree(N);
    std::vector<int> indeg(N);
    for(int i = 0; i < N-1; ++i){
      int a, b; std::cin >> a >> b;
      --a, --b;
      add_edge(tree, a, b, 1);
      indeg[b] += 1;
    }

    int root = std::find(indeg.begin(), indeg.end(), 0) - indeg.begin();

    std::vector<mint> sum(N);
    std::vector<int> sub(N);

    auto f =
      make_fix_point(
        [&](auto &&f, int cur) -> mint{
          mint ret = 0;

          sub[cur] = 1;

          for(auto &e : tree[cur]){
            ret += f(e.to);
            sub[cur] += sub[e.to];
          }

          ret += sub[cur] - 1;

          return sum[cur] = ret;
        }
      );

    f(root);

    auto ans = std::accumulate(sum.begin(), sum.end(), mint(0));
    dump(sub, sum);
    
    std::cout << ans << "\n";
  }
  

  return 0;
}
0