結果

問題 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
コンパイル時間 950 ms
コンパイル使用メモリ 83,208 KB
実行使用メモリ 65,664 KB
最終ジャッジ日時 2024-06-01 15:44:45
合計ジャッジ時間 13,206 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 AC 502 ms
46,428 KB
testcase_04 AC 595 ms
30,416 KB
testcase_05 AC 1,029 ms
50,432 KB
testcase_06 AC 346 ms
20,956 KB
testcase_07 AC 69 ms
7,424 KB
testcase_08 AC 106 ms
9,728 KB
testcase_09 AC 41 ms
5,888 KB
testcase_10 AC 155 ms
12,160 KB
testcase_11 AC 624 ms
32,896 KB
testcase_12 AC 344 ms
21,192 KB
testcase_13 AC 153 ms
12,464 KB
testcase_14 AC 31 ms
5,376 KB
testcase_15 AC 255 ms
17,152 KB
testcase_16 AC 692 ms
36,608 KB
testcase_17 AC 732 ms
38,016 KB
testcase_18 AC 151 ms
12,128 KB
testcase_19 AC 636 ms
33,664 KB
testcase_20 AC 49 ms
6,528 KB
testcase_21 AC 95 ms
9,032 KB
testcase_22 AC 504 ms
28,160 KB
testcase_23 AC 282 ms
18,144 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