結果
問題 | No.1103 Directed Length Sum |
ユーザー | snow39 |
提出日時 | 2020-07-03 21:53:25 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 650 ms / 3,000 ms |
コード長 | 1,146 bytes |
コンパイル時間 | 1,017 ms |
コンパイル使用メモリ | 101,036 KB |
実行使用メモリ | 120,384 KB |
最終ジャッジ日時 | 2024-09-17 00:25:28 |
合計ジャッジ時間 | 7,758 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
ソースコード
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} vector<vector<int>> g; vector<int> sz; void szdfs(int now){ sz[now]=1; for(auto nex:g[now]){ szdfs(nex); sz[now]+=sz[nex]; } } ll ans; void dfs(int now,ll dep){ for(auto nex:g[now]){ ans+=(dep+1)*sz[nex]; ans%=MOD; dfs(nex,dep+1); } } int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin>>N; g.resize(N); vector<int> A(N-1),B(N-1); rep(i,N-1){ cin>>A[i]>>B[i]; A[i]--;B[i]--; g[A[i]].push_back(B[i]); } vector<int> num(N,0); rep(i,N-1) num[B[i]]++; int root=0; rep(i,N) if(num[i]==0) root=i; sz.resize(N); szdfs(root); dfs(root,0); cout<<ans%MOD<<endl; return 0; }