結果
問題 |
No.1103 Directed Length Sum
|
ユーザー |
![]() |
提出日時 | 2021-10-12 03:01:38 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 927 ms / 3,000 ms |
コード長 | 1,217 bytes |
コンパイル時間 | 1,502 ms |
コンパイル使用メモリ | 143,352 KB |
実行使用メモリ | 61,876 KB |
最終ジャッジ日時 | 2024-09-16 10:27:26 |
合計ジャッジ時間 | 10,606 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
ソースコード
#include <cassert> #include <cmath> #include <algorithm> #include <iostream> #include <iomanip> #include <limits.h> #include <map> #include <queue> #include <set> #include <string.h> #include <vector> using namespace std; typedef long long ll; typedef pair<int, ll> Node; const ll MOD = 1000000007; const int MAX_N = 1000010; vector<int> E[MAX_N]; int P[MAX_N]; int N; int find_root(int v) { if (P[v] == -1) return v; return find_root(P[v]); } ll dfs(int v, int depth) { ll len = 0; for (int u : E[v]) { len += depth * (depth + 1) / 2; len %= MOD; len += dfs(u, depth + 1); len %= MOD; } return len; } int main() { memset(P, -1, sizeof(P)); cin >> N; for (int i = 0; i < N - 1; ++i) { int a, b; cin >> a >> b; E[a].push_back(b); P[b] = a; } int root = find_root(1); ll ans = 0; // cout << dfs(root, 1) << endl; queue<Node> que; que.push(Node(root, 1)); while (not que.empty()) { Node node = que.front(); int v = node.first; ll depth = node.second; que.pop(); for (int u : E[v]) { ans += depth * (depth + 1) / 2; ans %= MOD; que.push(Node(u, depth + 1)); } } cout << ans << endl; return 0; }