結果

問題 No.1103 Directed Length Sum
ユーザー umezoumezo
提出日時 2020-07-03 23:27:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 693 bytes
コンパイル時間 2,846 ms
コンパイル使用メモリ 238,768 KB
実行使用メモリ 304,184 KB
最終ジャッジ日時 2023-10-17 06:24:27
合計ジャッジ時間 15,753 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
69,752 KB
testcase_01 AC 42 ms
69,752 KB
testcase_02 WA -
testcase_03 AC 557 ms
79,448 KB
testcase_04 AC 704 ms
80,312 KB
testcase_05 AC 1,238 ms
87,968 KB
testcase_06 AC 465 ms
76,616 KB
testcase_07 AC 130 ms
71,336 KB
testcase_08 AC 187 ms
72,392 KB
testcase_09 AC 96 ms
70,808 KB
testcase_10 AC 247 ms
73,184 KB
testcase_11 AC 772 ms
81,368 KB
testcase_12 AC 468 ms
76,616 KB
testcase_13 AC 248 ms
73,184 KB
testcase_14 AC 81 ms
70,544 KB
testcase_15 AC 370 ms
75,032 KB
testcase_16 AC 874 ms
82,688 KB
testcase_17 AC 906 ms
83,216 KB
testcase_18 AC 244 ms
73,184 KB
testcase_19 AC 790 ms
81,632 KB
testcase_20 AC 108 ms
71,072 KB
testcase_21 AC 170 ms
72,128 KB
testcase_22 AC 657 ms
79,256 KB
testcase_23 AC 393 ms
75,560 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:40:6: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
   40 |   rec(root,0);
      |   ~~~^~~~~~~~
main.cpp:32:7: note: 'root' was declared here
   32 |   int 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;

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

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

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

  return 0;
}
0