結果

問題 No.1103 Directed Length Sum
ユーザー snow39snow39
提出日時 2020-07-03 21:53:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 603 ms / 3,000 ms
コード長 1,146 bytes
コンパイル時間 958 ms
コンパイル使用メモリ 99,852 KB
実行使用メモリ 120,536 KB
最終ジャッジ日時 2023-10-17 01:50:24
合計ジャッジ時間 7,346 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 330 ms
120,536 KB
testcase_03 AC 183 ms
46,464 KB
testcase_04 AC 304 ms
34,228 KB
testcase_05 AC 603 ms
56,980 KB
testcase_06 AC 174 ms
23,340 KB
testcase_07 AC 35 ms
8,232 KB
testcase_08 AC 54 ms
10,608 KB
testcase_09 AC 22 ms
6,384 KB
testcase_10 AC 80 ms
13,444 KB
testcase_11 AC 352 ms
37,060 KB
testcase_12 AC 184 ms
23,604 KB
testcase_13 AC 79 ms
13,708 KB
testcase_14 AC 16 ms
5,592 KB
testcase_15 AC 131 ms
19,184 KB
testcase_16 AC 411 ms
41,216 KB
testcase_17 AC 426 ms
42,996 KB
testcase_18 AC 72 ms
13,444 KB
testcase_19 AC 371 ms
38,116 KB
testcase_20 AC 24 ms
6,912 KB
testcase_21 AC 46 ms
10,080 KB
testcase_22 AC 260 ms
31,652 KB
testcase_23 AC 136 ms
20,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
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;}

vector<vector<int>> g;
vector<int> sz;

void szdfs(int now){
  sz[now]=1;
  for(auto nex:g[now]){
    szdfs(nex);
    sz[now]+=sz[nex];
  }
}

ll ans;

void dfs(int now,ll dep){
  for(auto nex:g[now]){
    ans+=(dep+1)*sz[nex];
    ans%=MOD;
    dfs(nex,dep+1);
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin>>N;
  g.resize(N);
  vector<int> A(N-1),B(N-1);
  rep(i,N-1){
    cin>>A[i]>>B[i];
    A[i]--;B[i]--;
    g[A[i]].push_back(B[i]);
  }

  vector<int> num(N,0);
  rep(i,N-1) num[B[i]]++;
  int root=0;
  rep(i,N) if(num[i]==0) root=i;

  sz.resize(N);
  szdfs(root);

  dfs(root,0);
  cout<<ans%MOD<<endl;

  return 0;
}
0