結果

問題 No.1103 Directed Length Sum
ユーザー umezo
提出日時 2020-07-03 23:23:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 696 bytes
コンパイル時間 2,623 ms
コンパイル使用メモリ 226,116 KB
最終ジャッジ日時 2025-01-11 15:05:57
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21 WA * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
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<int> D(1000010);  
vector<vector<int>> 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);
    
  int ans=0;
  for(int i=1;i<=n;i++){
    ans+=D[i]*(D[i]+1)/2;
  }
  
  cout<<ans<<endl;

  return 0;
}
0