結果
問題 | No.1103 Directed Length Sum |
ユーザー | kurotemko |
提出日時 | 2020-07-26 21:28:12 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,010 ms / 3,000 ms |
コード長 | 1,523 bytes |
コンパイル時間 | 2,428 ms |
コンパイル使用メモリ | 205,944 KB |
実行使用メモリ | 151,644 KB |
最終ジャッジ日時 | 2023-09-11 05:05:12 |
合計ジャッジ時間 | 12,043 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,380 KB |
testcase_01 | AC | 2 ms
4,380 KB |
testcase_02 | AC | 406 ms
151,644 KB |
testcase_03 | AC | 274 ms
81,268 KB |
testcase_04 | AC | 527 ms
46,092 KB |
testcase_05 | AC | 1,010 ms
77,572 KB |
testcase_06 | AC | 329 ms
31,088 KB |
testcase_07 | AC | 52 ms
10,092 KB |
testcase_08 | AC | 100 ms
13,320 KB |
testcase_09 | AC | 32 ms
7,336 KB |
testcase_10 | AC | 147 ms
17,264 KB |
testcase_11 | AC | 592 ms
49,996 KB |
testcase_12 | AC | 334 ms
31,404 KB |
testcase_13 | AC | 154 ms
17,616 KB |
testcase_14 | AC | 23 ms
6,272 KB |
testcase_15 | AC | 251 ms
25,120 KB |
testcase_16 | AC | 688 ms
56,020 KB |
testcase_17 | AC | 723 ms
58,092 KB |
testcase_18 | AC | 148 ms
17,076 KB |
testcase_19 | AC | 604 ms
51,272 KB |
testcase_20 | AC | 39 ms
8,436 KB |
testcase_21 | AC | 89 ms
12,268 KB |
testcase_22 | AC | 482 ms
42,548 KB |
testcase_23 | AC | 266 ms
26,712 KB |
ソースコード
// 解説とテスターの方のコードを参考 // https://jupiro.hatenablog.com/entry/yukicoder1103 // dpは使わず、(頂点bの深さ)×(頂点b以下の頂点の数)の和を求めた // ちゃんとlong longなどで、大きな値に対応しないとテストで落ちる #include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; vector<vector<long long>> g; vector<long long> depth; vector<long long> child; // 各頂点の深さと、ある頂点における自分と子の数を求める void dfs(int cur, int pre) { child[cur] = 1; for(auto next : g[cur]) { if(next == pre) continue; depth[next] = depth[cur] + 1; dfs(next, cur); child[cur] += child[next]; } return; } int main() { int n; cin >> n; // nの長さを確保 g.resize(n); depth.resize(n); child.resize(n); // 根を探す用の配列 vector<bool> s_root(n, true); // グラフ作成 for(int i = 0; i < n-1; ++i) { int a, b; scanf("%d %d", &a, &b); a--; b--; g[a].emplace_back(b); g[b].emplace_back(a); s_root[b] = false; } // 根を探す int root = -1; for(int i = 0; i < n; ++i) if(s_root[i] == true) root = i; // 根から探索 dfs(root, -1); // 和の計算 long long ans = 0; for(int i = 0; i < n; ++i) { ans += (depth[i]%mod*child[i]%mod) % mod; ans %= mod; } cout << ans << endl; return 0; }