結果

問題 No.1103 Directed Length Sum
ユーザー chocono2230chocono2230
提出日時 2020-07-03 22:21:49
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,029 ms / 3,000 ms
コード長 2,292 bytes
コンパイル時間 1,635 ms
コンパイル使用メモリ 173,328 KB
実行使用メモリ 108,684 KB
最終ジャッジ日時 2023-10-17 04:30:56
合計ジャッジ時間 11,742 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 738 ms
108,684 KB
testcase_03 AC 490 ms
36,096 KB
testcase_04 AC 571 ms
27,780 KB
testcase_05 AC 1,029 ms
45,588 KB
testcase_06 AC 354 ms
19,204 KB
testcase_07 AC 72 ms
7,264 KB
testcase_08 AC 116 ms
9,112 KB
testcase_09 AC 43 ms
5,680 KB
testcase_10 AC 166 ms
11,420 KB
testcase_11 AC 630 ms
30,088 KB
testcase_12 AC 367 ms
19,468 KB
testcase_13 AC 177 ms
11,684 KB
testcase_14 AC 32 ms
5,152 KB
testcase_15 AC 281 ms
15,840 KB
testcase_16 AC 717 ms
33,452 KB
testcase_17 AC 755 ms
34,508 KB
testcase_18 AC 170 ms
11,420 KB
testcase_19 AC 651 ms
30,616 KB
testcase_20 AC 53 ms
6,208 KB
testcase_21 AC 105 ms
8,584 KB
testcase_22 AC 522 ms
25,668 KB
testcase_23 AC 301 ms
16,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int 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;
  }
};

void dfs(vector<vector<int>> &tree, int now, int nt, mint &ans){
  mint add = (ll)nt * (nt+1)/2;
  ans += add;
  if(tree.at(now).size() == 0){
    return ;
  }
  for(auto i : tree.at(now)){
    dfs(tree, i, nt+1, ans);
  }
  return ;
}

int main(){
  int n;
  cin >> n;
  vector<vector<int>> tree(n, vector<int>());
  vector<int> indeg(n, 0);
  rep(i, n-1){
    int a, b;
    cin >> a >> b;
    a--; b--;
    tree.at(a).push_back(b);
    indeg.at(b)++;
  }
  int root = -1;
  rep(i, n) if(indeg.at(i) == 0){
    root = i;
    break;
  }
  mint ans = 0;
  dfs(tree, root, 0, ans);
  cout << ans.x  << endl;
  return 0;
}
0