結果

問題 No.1103 Directed Length Sum
ユーザー yukudoyukudo
提出日時 2020-07-04 03:45:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 670 ms / 3,000 ms
コード長 1,321 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 172,568 KB
実行使用メモリ 96,152 KB
最終ジャッジ日時 2023-10-17 10:30:01
合計ジャッジ時間 10,747 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
30,056 KB
testcase_01 AC 12 ms
30,056 KB
testcase_02 AC 339 ms
96,152 KB
testcase_03 AC 223 ms
38,208 KB
testcase_04 AC 369 ms
41,044 KB
testcase_05 AC 670 ms
49,160 KB
testcase_06 AC 228 ms
37,084 KB
testcase_07 AC 51 ms
31,540 KB
testcase_08 AC 76 ms
32,332 KB
testcase_09 AC 36 ms
30,956 KB
testcase_10 AC 115 ms
33,388 KB
testcase_11 AC 413 ms
42,100 KB
testcase_12 AC 232 ms
37,084 KB
testcase_13 AC 110 ms
33,388 KB
testcase_14 AC 27 ms
30,732 KB
testcase_15 AC 172 ms
35,500 KB
testcase_16 AC 458 ms
43,420 KB
testcase_17 AC 473 ms
44,212 KB
testcase_18 AC 98 ms
33,388 KB
testcase_19 AC 404 ms
42,364 KB
testcase_20 AC 38 ms
31,144 KB
testcase_21 AC 65 ms
32,068 KB
testcase_22 AC 328 ms
39,988 KB
testcase_23 AC 185 ms
35,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}

const ll MOD = 1e9 + 7;


const int MAX_N = 1123456;
vector<int> g[MAX_N];

ll ans;
int dfs(int u, int depth) {

  int sum_child = 0;

  for (int v : g[u]) {
    int num_child = dfs(v, depth + 1);
    sum_child += num_child;
    ans += (ll)(depth + 1) * (ll)num_child;
    ans %= MOD;
  }

  return sum_child + 1;
}


int main2() {
  int N = nextLong();

  vector<int> indeg(N);
  REP(i, N-1) {
    int a = nextLong() - 1;
    int b = nextLong() - 1;
    g[a].push_back(b);
    ++indeg[b];
  }

  int root = 0;
  REP(i, N) if (indeg[i] == 0) root = i;

  ans = 0;
  dfs(root, 0);

  ans %= MOD;
  cout << ans << endl;

  REP(i, N) g[i].clear();
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}
0