結果

問題 No.1103 Directed Length Sum
ユーザー umezo
提出日時 2020-07-03 23:45:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,158 ms / 3,000 ms
コード長 724 bytes
コンパイル時間 2,261 ms
コンパイル使用メモリ 227,488 KB
最終ジャッジ日時 2025-01-11 15:12:04
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
      |      ^~~~

ソースコード

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;

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=ans+((D[i]*(D[i]+1))/2)%MOD;
  }
  
  cout<<ans%MOD<<endl;

  return 0;
}
0