結果
問題 |
No.399 動的な領主
|
ユーザー |
|
提出日時 | 2016-07-19 11:30:35 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 115 ms / 2,000 ms |
コード長 | 2,694 bytes |
コンパイル時間 | 1,471 ms |
コンパイル使用メモリ | 169,444 KB |
実行使用メモリ | 21,632 KB |
最終ジャッジ日時 | 2024-11-07 19:39:58 |
合計ジャッジ時間 | 3,481 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 19 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:131:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 131 | scanf("%d %d", &u, &v); | ~~~~~^~~~~~~~~~~~~~~~~ main.cpp:144:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 144 | scanf("%d %d", &u, &v); | ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h> using namespace std; class HLDecomposition { public: HLDecomposition(int n) : g(n), vid(n), sub(n, 1), head(n), heavy(n, -1), parent(n) { } void add(int u, int v) { g[u].push_back(v); g[v].push_back(u); } void build() { dfs(0, -1); bfs(); } 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); } int lca(int u, int v) { if (vid[u] > vid[v]) swap(u, v); if (head[u] == head[v]) return u; return lca(u, parent[head[v]]); } vector<int> parent; private: vector<vector<int>> g; vector<int> vid; vector<int> sub; vector<int> head; vector<int> heavy; 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; 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; }