結果

問題 No.1103 Directed Length Sum
ユーザー CleyLCleyL
提出日時 2022-09-18 21:53:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,015 ms / 3,000 ms
コード長 719 bytes
コンパイル時間 1,191 ms
コンパイル使用メモリ 83,064 KB
実行使用メモリ 65,536 KB
最終ジャッジ日時 2023-08-23 18:20:37
合計ジャッジ時間 11,148 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 663 ms
65,536 KB
testcase_03 AC 457 ms
46,388 KB
testcase_04 AC 558 ms
30,168 KB
testcase_05 AC 1,015 ms
50,212 KB
testcase_06 AC 346 ms
20,812 KB
testcase_07 AC 65 ms
7,312 KB
testcase_08 AC 100 ms
9,624 KB
testcase_09 AC 39 ms
5,668 KB
testcase_10 AC 149 ms
11,820 KB
testcase_11 AC 599 ms
32,696 KB
testcase_12 AC 336 ms
21,032 KB
testcase_13 AC 153 ms
12,272 KB
testcase_14 AC 29 ms
5,124 KB
testcase_15 AC 257 ms
16,764 KB
testcase_16 AC 703 ms
36,588 KB
testcase_17 AC 731 ms
37,760 KB
testcase_18 AC 149 ms
11,932 KB
testcase_19 AC 633 ms
33,428 KB
testcase_20 AC 46 ms
6,136 KB
testcase_21 AC 92 ms
9,024 KB
testcase_22 AC 503 ms
27,904 KB
testcase_23 AC 284 ms
17,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

const long long MOD = 1000000007;
struct d{
  int i,dep;
};

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%MOD << endl;
  

}
0