結果
問題 | No.922 東北きりきざむたん |
ユーザー | 🍮かんプリン |
提出日時 | 2020-09-29 20:03:43 |
言語 | C++11 (gcc 11.4.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,917 bytes |
コンパイル時間 | 1,833 ms |
コンパイル使用メモリ | 184,052 KB |
実行使用メモリ | 48,980 KB |
最終ジャッジ日時 | 2024-07-04 10:15:03 |
合計ジャッジ時間 | 7,049 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 3 ms
5,376 KB |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | AC | 25 ms
15,232 KB |
testcase_13 | AC | 17 ms
6,272 KB |
testcase_14 | WA | - |
testcase_15 | AC | 13 ms
16,000 KB |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 240 ms
24,788 KB |
testcase_25 | AC | 219 ms
26,084 KB |
testcase_26 | AC | 200 ms
26,080 KB |
testcase_27 | AC | 200 ms
26,076 KB |
testcase_28 | AC | 70 ms
18,444 KB |
testcase_29 | AC | 256 ms
48,980 KB |
ソースコード
/** * @FileName a.cpp * @Author kanpurin * @Created 2020.09.29 20:03:32 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; struct LowestCommonAncestor { private: int n; int log; vector<vector<int>> parent; vector<int> dep; vector<vector<int>> G; void dfs(const vector<vector<int>> &G, int v, int p, int d) { parent[0][v] = p; dep[v] = d; for (int to : G[v]) { if (to != p) dfs(G, to, v, d + 1); } } public: LowestCommonAncestor(int n) : n(n) { G.resize(n); } void add_edge(int from, int to) { G[from].push_back(to); G[to].push_back(from); } void build(int root = 0) { log = log2(n) + 1; parent.resize(log, vector<int>(n)); dep.resize(n); dfs(G, root, -1, 0); for (int k = 0; k + 1 < log; k++) { for (int v = 0; v < G.size(); v++) { if (parent[k][v] < 0) { parent[k + 1][v] = -1; } else { parent[k + 1][v] = parent[k][parent[k][v]]; } } } } int depth(int v) { return dep[v]; } int lca(int u, int v) { if (dep[u] > dep[v]) swap(u, v); for (int k = 0; k < log; k++) if ((dep[v] - dep[u]) >> k & 1) v = parent[k][v]; if (u == v) return u; for (int k = log - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } }; class UnionFind { private: vector<int> par; public: UnionFind(int n) { par.resize(n, -1); } int root(int x) { if (par[x] < 0) return x; return par[x] = root(par[x]); } bool unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return false; if (size(rx) < size(ry)) swap(rx, ry); par[rx] += par[ry]; par[ry] = rx; return true; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } int size(int x) { return -par[root(x)]; } }; using T = ll; int n; vector<vector<int>> tree; vector<T> dist; vector<T> dist2; vector<T> ans; vector<T> cost; vector<bool> used; void dfs1(int v, int p = -1) { used[v] = true; T res = 0, res2 = cost[v]; for (auto &u : tree[v]) { if (u == p) continue; dfs1(u, v); res += dist[u] + dist2[u]; res2 += dist2[u]; } dist2[v] = res2; dist[v] = res; } T dfs2(int v, T d_par, T d_par2, int p = -1) { used[v] = true; ans[v] = d_par + dist[v] + d_par2; T res = ans[v]; auto f = [](T a, T b) { return a + b; }; T e = 0; vector<T> lf(tree[v].size(), e), ri(tree[v].size(), e); vector<T> lf2(tree[v].size(), e), ri2(tree[v].size(), e); for (int i = 1; i < tree[v].size(); i++) { if (tree[v][i - 1] == p) lf[i] = f(d_par, lf[i - 1]); else lf[i] = f(dist[tree[v][i - 1]], lf[i - 1]); if (tree[v][i - 1] == p) lf2[i] = f(d_par2, lf2[i - 1]); else lf2[i] = f(dist2[tree[v][i - 1]], lf2[i - 1]); } for (int i = tree[v].size() - 2; i >= 0; i--) { if (tree[v][i + 1] == p) ri[i] = f(d_par, ri[i + 1]); else ri[i] = f(dist[tree[v][i + 1]], ri[i + 1]); if (tree[v][i + 1] == p) ri2[i] = f(d_par2, ri2[i + 1]); else ri2[i] = f(dist2[tree[v][i + 1]], ri2[i + 1]); } for (int i = 0; i < tree[v].size(); i++) { if (tree[v][i] == p) continue; res = min(res,dfs2(tree[v][i], f(lf[i], ri[i]) + f(lf2[i], ri2[i]), f(lf2[i], ri2[i]) + cost[v], v)); } return res; } int main(){ int m,q;cin >> n >> m >> q; tree.resize(n); dist.resize(n); dist2.resize(n); ans.resize(n); cost.resize(n,0); UnionFind uf(n); LowestCommonAncestor lca(n); ll sum = 0; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; tree[u].push_back(v); tree[v].push_back(u); uf.unite(u,v); lca.add_edge(u,v); } lca.build(); for (int i = 0; i < q; i++) { int a,b;cin >> a >> b; a--;b--; if (uf.same(a,b)) { sum += lca.dist(a,b); } else { cost[a]++; cost[b]++; } } used.resize(n,false); for (int i = 0; i < n; i++) { if (used[i]) continue; dfs1(i); } used.assign(n,false); for (int i = 0; i < n; i++) { if (used[i]) continue; sum += dfs2(i, 0, 0); } cout << sum << endl; return 0; }