結果
問題 | No.1103 Directed Length Sum |
ユーザー | itohdak |
提出日時 | 2020-07-03 22:22:58 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 782 ms / 3,000 ms |
コード長 | 957 bytes |
コンパイル時間 | 1,798 ms |
コンパイル使用メモリ | 197,988 KB |
最終ジャッジ日時 | 2025-01-11 14:50:42 |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define ll long long #define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--) #define rrep(i,n) RREP(i,n-1,0) #define all(v) v.begin(), v.end() const int inf = 1e9+7; const ll longinf = 1LL<<60; const ll mod = 1e9+7; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<vector<int>> G(n); vector<vector<int>> from(n); rep(i, n-1) { int a, b; cin >> a >> b; a--; b--; G[a].push_back(b); from[b].push_back(a); } int root = 0; rep(i, n) if(from[i].size() == 0) root = i; ll ans = 0; ll tmp = 0; auto dfs = [&](auto dfs, int cur, int par, ll depth) -> void { ll in = tmp; tmp++; for(int ne: G[cur]) dfs(dfs, ne, cur, depth+1); ll out = tmp; (ans += (out-in) * depth) %= mod; }; dfs(dfs, root, -1, 0); cout << ans << "\n"; return 0; }