結果
問題 |
No.1103 Directed Length Sum
|
ユーザー |
![]() |
提出日時 | 2021-10-12 03:00:15 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,219 bytes |
コンパイル時間 | 1,135 ms |
コンパイル使用メモリ | 144,700 KB |
実行使用メモリ | 61,980 KB |
最終ジャッジ日時 | 2024-09-16 10:25:25 |
合計ジャッジ時間 | 10,838 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 21 WA * 1 |
ソースコード
#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, int> 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; int 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; }