結果

問題 No.1103 Directed Length Sum
ユーザー CleyLCleyL
提出日時 2022-09-18 21:38:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 680 bytes
コンパイル時間 903 ms
コンパイル使用メモリ 82,628 KB
実行使用メモリ 65,520 KB
最終ジャッジ日時 2023-08-23 18:20:09
合計ジャッジ時間 12,552 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,000 KB
testcase_01 AC 2 ms
5,004 KB
testcase_02 WA -
testcase_03 AC 473 ms
46,256 KB
testcase_04 AC 560 ms
30,140 KB
testcase_05 AC 1,022 ms
50,244 KB
testcase_06 AC 320 ms
20,616 KB
testcase_07 AC 61 ms
7,472 KB
testcase_08 AC 95 ms
9,584 KB
testcase_09 AC 39 ms
5,708 KB
testcase_10 AC 139 ms
11,888 KB
testcase_11 AC 585 ms
32,712 KB
testcase_12 AC 319 ms
20,896 KB
testcase_13 AC 143 ms
12,360 KB
testcase_14 AC 29 ms
5,168 KB
testcase_15 AC 257 ms
17,104 KB
testcase_16 AC 695 ms
36,412 KB
testcase_17 AC 745 ms
37,932 KB
testcase_18 AC 147 ms
11,920 KB
testcase_19 AC 631 ms
33,324 KB
testcase_20 AC 45 ms
6,392 KB
testcase_21 AC 86 ms
8,896 KB
testcase_22 AC 485 ms
27,944 KB
testcase_23 AC 271 ms
17,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
struct d{
  int i,dep;
};
using namespace std;

int main(){
  int n;cin>>n;
  vector<long long> A(n,-1);
  vector<int> C[n];
  for(int i = 0; n-1 > i; i++){
    int a,b;cin>>a>>b;
    A[--b] = --a;
    C[a].push_back(b);
  }
  int f = -1;
  for(int i = 0; n > i; i++){
    if(A[i] == -1)f = i;
  }

  queue<d> B;
  B.push({f,0});
  A[f] = 0;
  while(B.size()){
    auto x = B.front();B.pop();
    for(int i = 0; C[x.i].size() > i; i++){
      A[C[x.i][i]] = x.dep+1;
      B.push({C[x.i][i],x.dep+1});
    } 
  }
  long long ans = 0;
  for(int i = 0; n > i; i++){
    ans += A[i]*(A[i]+1)/2;
  }
  cout << ans << endl;
  

}
0