結果

問題 No.1103 Directed Length Sum
ユーザー ate
提出日時 2020-07-03 23:23:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,466 ms / 3,000 ms
コード長 719 bytes
コンパイル時間 3,060 ms
コンパイル使用メモリ 198,348 KB
最終ジャッジ日時 2025-01-11 15:05:34
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)

signed main(){

  int n;
  cin>>n;
  vector<vector<int>> adj(n);
  vector<int> par(n,-1);
  rep(i,n-1){
    int a,b;
    cin>>a>>b;
    a--;b--;
    adj[a].emplace_back(b);
    par[b] = a;
  }
  
  int root = -1;
  rep(i,n)if(par[i]==-1)root=i;
  // pars,childs
  constexpr int64_t mod = 1e9+7;
  int64_t ans = 0;
  auto dfs = [&](auto&&dfs,int cur,int pnum)->int64_t{
    int64_t cnum = 0;
    for(auto to:adj[cur]){
      int64_t tmp = dfs(dfs,to,pnum+1);
      cnum += tmp;
    }
    //cout<<cur<<" "<<pnum<<" "<<cnum<<endl;
    ans += pnum*cnum;
    ans %= mod;
    return cnum+1;
  };
  dfs(dfs,root,1);
  cout<<(ans)<<endl;

}
0