結果
問題 | No.399 動的な領主 |
ユーザー | pekempey |
提出日時 | 2016-07-19 12:08:41 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 114 ms / 2,000 ms |
コード長 | 3,499 bytes |
コンパイル時間 | 1,575 ms |
コンパイル使用メモリ | 171,624 KB |
実行使用メモリ | 24,076 KB |
最終ジャッジ日時 | 2024-11-07 19:40:02 |
合計ジャッジ時間 | 3,765 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
5,888 KB |
testcase_01 | AC | 3 ms
5,888 KB |
testcase_02 | AC | 3 ms
5,888 KB |
testcase_03 | AC | 3 ms
5,888 KB |
testcase_04 | AC | 4 ms
6,016 KB |
testcase_05 | AC | 11 ms
7,168 KB |
testcase_06 | AC | 114 ms
19,592 KB |
testcase_07 | AC | 114 ms
19,472 KB |
testcase_08 | AC | 108 ms
19,592 KB |
testcase_09 | AC | 107 ms
19,724 KB |
testcase_10 | AC | 4 ms
6,016 KB |
testcase_11 | AC | 9 ms
7,296 KB |
testcase_12 | AC | 85 ms
20,508 KB |
testcase_13 | AC | 88 ms
20,488 KB |
testcase_14 | AC | 74 ms
23,948 KB |
testcase_15 | AC | 76 ms
24,076 KB |
testcase_16 | AC | 77 ms
21,516 KB |
testcase_17 | AC | 113 ms
19,596 KB |
testcase_18 | AC | 113 ms
19,596 KB |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:162:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 162 | scanf("%d %d", &u, &v); | ~~~~~^~~~~~~~~~~~~~~~~ main.cpp:175:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 175 | scanf("%d %d", &u, &v); | ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h> using namespace std; struct HLDecomposition { vector<vector<int>> g; vector<int> vid; vector<int> sub; vector<int> head; vector<int> heavy; vector<int> parent; vector<int> lca_depth; vector<vector<int>> lca_parent; HLDecomposition(int n) : g(n), vid(n), sub(n, 1), head(n), heavy(n, -1), parent(n), lca_depth(n), lca_parent(5, vector<int>(n, -1)) { } void add(int u, int v) { g[u].push_back(v); g[v].push_back(u); } void build() { dfs(0, -1); bfs(); for (int i = 0; i < 4; i++) { for (int j = 0; j < g.size(); j++) { if (lca_parent[i][j] != -1) { lca_parent[i + 1][j] = lca_parent[i][lca_parent[i][j]]; } } } } int lca(int u, int v) { if (vid[u] > vid[v]) swap(u, v); if (head[u] == head[v]) return u; int U = u; u = head[u]; v = head[v]; for (int i = 4; i >= 0; i--) { if (lca_depth[v] - lca_depth[u] >= 1 << i) { int nv = lca_parent[i][v]; if (u != nv) v = nv; } } if (lca_parent[0][v] == u) { return vid[parent[v]] < vid[U] ? parent[v] : U; } for (int i = 4; i >= 0; i--) { if (lca_parent[i][u] != lca_parent[i][v]) { u = lca_parent[i][u]; v = lca_parent[i][v]; } } u = parent[u]; v = parent[v]; return vid[u] < vid[v] ? u : v; } struct Iterator { int u, v; HLDecomposition *hl; Iterator(HLDecomposition *hl, int u, int v) : hl(hl), u(u), v(v) {} pair<int, int> operator*() { if (hl->vid[u] > hl->vid[v]) swap(u, v); int l = max(hl->vid[hl->head[v]], hl->vid[u]); int r = hl->vid[v]; v = hl->head[u] != hl->head[v] ? hl->parent[hl->head[v]] : -1; return make_pair(l, r + 1); } void operator++() {} bool operator!=(Iterator &) { return v != -1; } }; struct Enumerator { int u, v; HLDecomposition *hl; Enumerator(HLDecomposition *hl, int u, int v) : hl(hl), u(u), v(v) {} Iterator begin() const { return Iterator(hl, u, v); } Iterator end() const { return Iterator(hl, u, v); } }; Enumerator enumerate(int u, int v) { return Enumerator(this, u, v); } void dfs(int curr, int prev) { parent[curr] = prev; for (int next : g[curr]) if (next != prev) { dfs(next, curr); sub[curr] += sub[next]; if (heavy[curr] == -1 || sub[heavy[curr]] < sub[next]) { heavy[curr] = next; } } } void bfs() { int k = 0; queue<int> q; q.push(0); while (!q.empty()) { int first = q.front(); q.pop(); for (int curr = first; curr != -1; curr = heavy[curr]) { vid[curr] = k++; head[curr] = first; for (int next : g[curr]) { if (next == parent[curr]) continue; if (next == heavy[curr]) continue; lca_depth[next] = lca_depth[first] + 1; lca_parent[0][next] = first; q.push(next); } } } } }; vector<int> g[101010]; long long imos[101010]; long long ans; void dfs(int curr, int prev) { for (int next : g[curr]) if (next != prev) { dfs(next, curr); } if (prev != -1) { imos[prev] += imos[curr]; } ans += imos[curr] * (imos[curr] + 1) / 2; } int main() { int n; cin >> n; HLDecomposition hl(n); for (int i = 0; i < n - 1; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; g[u].push_back(v); g[v].push_back(u); hl.add(u, v); } hl.build(); int Q; cin >> Q; while (Q--) { int u, v; scanf("%d %d", &u, &v); u--; v--; int l = hl.lca(u, v); imos[u]++; imos[v]++; imos[l]--; if (hl.parent[l] != -1) { imos[hl.parent[l]]--; } } dfs(0, -1); cout << ans << endl; }