結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  umezo | 
| 提出日時 | 2020-07-03 23:32:55 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 715 bytes | 
| コンパイル時間 | 2,241 ms | 
| コンパイル使用メモリ | 227,976 KB | 
| 最終ジャッジ日時 | 2025-01-11 15:09:05 | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 21 WA * 1 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:42:6: warning: ‘root’ may be used uninitialized [-Wmaybe-uninitialized]
   42 |   rec(root,0);
      |   ~~~^~~~~~~~
main.cpp:34:6: note: ‘root’ was declared here
   34 |   ll root;
      |      ^~~~
            
            ソースコード
#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;
const int MOD=1e9+7;
vector<ll> D(1000010);  
vector<vector<ll>> C(1000010);
void rec(ll u, ll p){
  D[u]=p;
  if(!C[u].empty()){
    for(auto x:C[u]) rec(x,p+1);
  }
}
int main() {
  int n;
  cin>>n;
  
  vector<ll> P(1000010);
  
  int a,b;
  rep(i,n){
    cin>>a>>b;
    P[b]=a;
    C[a].push_back(b);
  }
  
  ll root;
  for(ll i=1;i<=n;i++){
    if(P[i]==0){
      root=i;
      break;
    }
  }
  
  rec(root,0);
    
  ll ans=0;
  for(ll i=1;i<=n;i++){
    ans+=(D[i]*(D[i]+1)/2)%MOD;
  }
  
  cout<<ans<<endl;
  return 0;
}
            
            
            
        