結果
問題 | No.1103 Directed Length Sum |
ユーザー |
![]() |
提出日時 | 2020-07-04 00:47:24 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 235 ms / 3,000 ms |
コード長 | 1,090 bytes |
コンパイル時間 | 849 ms |
コンパイル使用メモリ | 95,992 KB |
最終ジャッジ日時 | 2025-01-11 15:17:03 |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
ソースコード
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> using namespace std; using ll = long long; ll ans = 0; struct Graph { struct Vertex { int n; }; struct Edge { int i, n; }; Graph(int n, int m) : v(n, { -1 }), e(m), n(n), m(0) {} void add_edge(int i, int j) { e[m] = { j, v[i].n }; v[i].n = m; m++; } void dfs(int i, int d) { for (int j = v[i].n; j >= 0; j = e[j].n) { Edge& o = e[j]; dfs(o.i, d + 1); } ans += (ll)d * (d + 1) / 2; } vector<Vertex> v; vector<Edge> e; int n, m; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; Graph g(n, (n - 1) * 2); int x = 0; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; g.add_edge(a, b); x ^= b; } for (int i = n & ~3; i < n; i++) { x ^= i; } g.dfs(x, 0); cout << ans % 1000000007 << endl; return 0; }