結果

問題 No.1103 Directed Length Sum
ユーザー umezoumezo
提出日時 2020-07-03 23:47:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,228 ms / 3,000 ms
コード長 713 bytes
コンパイル時間 3,238 ms
コンパイル使用メモリ 236,912 KB
実行使用メモリ 307,996 KB
最終ジャッジ日時 2023-10-17 06:29:23
合計ジャッジ時間 15,747 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
73,564 KB
testcase_01 AC 43 ms
73,564 KB
testcase_02 AC 1,028 ms
307,996 KB
testcase_03 AC 563 ms
83,156 KB
testcase_04 AC 707 ms
84,124 KB
testcase_05 AC 1,228 ms
91,780 KB
testcase_06 AC 469 ms
80,428 KB
testcase_07 AC 133 ms
75,412 KB
testcase_08 AC 188 ms
76,204 KB
testcase_09 AC 97 ms
74,620 KB
testcase_10 AC 246 ms
76,996 KB
testcase_11 AC 773 ms
85,180 KB
testcase_12 AC 470 ms
80,692 KB
testcase_13 AC 250 ms
77,260 KB
testcase_14 AC 82 ms
74,356 KB
testcase_15 AC 366 ms
79,108 KB
testcase_16 AC 855 ms
86,500 KB
testcase_17 AC 900 ms
87,028 KB
testcase_18 AC 243 ms
76,996 KB
testcase_19 AC 785 ms
85,444 KB
testcase_20 AC 109 ms
74,884 KB
testcase_21 AC 173 ms
75,940 KB
testcase_22 AC 649 ms
83,332 KB
testcase_23 AC 395 ms
79,372 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:42:6: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
   42 |   rec(root,0);
      |   ~~~^~~~~~~~
main.cpp:34:6: note: 'root' was declared here
   34 |   ll root;
      |      ^~~~

ソースコード

diff #

#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

const int MOD=1e9+7;

vector<ll> D(1000010);  
vector<vector<ll>> C(1000010);

void rec(ll u, ll p){
  D[u]=p;
  if(!C[u].empty()){
    for(auto x:C[u]) rec(x,p+1);
  }
}

int main() {
  int n;
  cin>>n;
  
  vector<ll> P(1000010);
  
  int a,b;
  rep(i,n){
    cin>>a>>b;
    P[b]=a;
    C[a].push_back(b);
  }
  
  ll root;
  for(ll i=1;i<=n;i++){
    if(P[i]==0){
      root=i;
      break;
    }
  }
  
  rec(root,0);
    
  ll ans=0;
  for(ll i=1;i<=n;i++){
    ans+=D[i]*(D[i]+1)/2;
  }
  
  cout<<ans%MOD<<endl;

  return 0;
}
0