#include<bits/stdc++.h> usingnamespacestd; #define N 500007 typedeflonglong ll;
vector<int> e[N]; int fa[N][21], n, p, rot;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
inlinevoiddfs(int u){ for (int i = 1; i <= t; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1]; for (auto cur : e[u]) { int v = cur.first, w = cur.second; if (v != fa[u][0]) { fa[v][0] = u; dep[v] = dep[u] + 1; dfs(v); } } }
intmain(){ n = rd(); q = rd(); rot = rd(); for (int i = 1; i <= n - 1; ++i) { int u = rd(), v = rd(); e[u].push_back(v); e[v].push_back(u); } fa[rot][0] = rot; dfs(rot); for (int i = 1; i <= 20; ++i) for (int j = 1; j <= n; ++j) fa[j][i] = fa[fa[j][i - 1]][i - 1]; for (int i = 1; i <= q; ++i) { int u = rd(), k = rd(); for (int j = 20; j >= 0; --j) if (k & (1 << i)) u = fa[u][i]; printf("%d\n", u); } return0; }
#include<bits/stdc++.h> usingnamespacestd; #define N 500007 typedeflonglong ll;
vector<int> e[N]; int fa[N][21], dep[N], t, n, m, rot;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
inlinevoiddfs(int u){ for (int i = 1; i <= t; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1]; for (auto cur : e[u]) { int v = cur.first, w = cur.second; if (v != fa[u][0]) { fa[v][0] = u; dep[v] = dep[u] + 1; sum[v] = sum[u] + w; dfs(v); } } }
inlineintlca(int u, int v){ if (dep[u] > dep[v]) swap(u, v); for (int i = t; i >= 0; --i) if (dep[fa[v][i]] >= dep[u]) v = fa[v][i]; if (u == v) return u; for (int i = t; i >= 0; --i) if (fa[u][i] != fa[v][i]) { u = fa[u][i]; v = fa[v][i]; } return fa[u][0]; }
intmain(){ n = rd(); m = rd(); rot = rd(); t = __lg(n - 1) + 1; for (int i = 1; i <= n - 1; ++i) { int u = rd(), v = rd(); e[u].push_back(v); e[v].push_back(u); } fa[rot][0] = rot; dfs(rot); for (int i = 1; i <= m; ++i) { int u = rd(), v = rd(); int ans = lca(u, v); printf("%d\n", ans); } return0; }
#include<bits/stdc++.h> usingnamespacestd; #define N 500007 typedeflonglong ll;
vector<pair<int, int> > e[N]; int fa[N][21], dep[N], sum[N], t, n, m, rot;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
inlinevoiddfs(int u){ for (int i = 1; i <= t; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1]; for (auto cur : e[u]) { int v = cur.first, w = cur.second; if (v != fa[u][0]) { fa[v][0] = u; dep[v] = dep[u] + 1; sum[v] = sum[u] + w; dfs(v); } } }
inlineintlca(int u, int v){ if (dep[u] > dep[v]) swap(u, v); for (int i = t; i >= 0; --i) if (dep[fa[v][i]] >= dep[u]) v = fa[v][i]; if (u == v) return u; for (int i = t; i >= 0; --i) if (fa[u][i] != fa[v][i]) { u = fa[u][i]; v = fa[v][i]; } return fa[u][0]; }
inlineintgetlen(int u, int v){ int ans = lca(u, v); return sum[u] + sum[v] - 2 * sum[ans]; }
inlinevoidwork(){ n = rd(); m = rd(); t = __lg(n - 1) + 1; for (int i = 1; i <= n - 1; ++i) { int u = rd(), v = rd(), w = rd(); e[u].push_back(make_pair(v, w)); e[v].push_back(make_pair(u, w)); } fa[1][0] = 1; dfs(1); for (int i = 1; i <= m; ++i) { int u = rd(), v = rd(); printf("%d\n", getlen(u, v)); } }
intmain(){ int t = rd(); while (t--) work(); return0; }
#include<bits/stdc++.h> usingnamespacestd; #define N 10007 typedeflonglong ll;
vector<pair<int, int> > e[N]; int fa[N][15], dep[N], sum[N], t, n, m, c;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
inlinevoiddfs(int u){ for (int i = 1; i <= t; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1]; for (auto cur : e[u]) { int v = cur.first, w = cur.second; if (v != fa[u][0]) { fa[v][0] = u; dep[v] = dep[u] + 1; sum[v] = sum[u] + w; dfs(v); } } }
inlineintlca(int u, int v){ if (dep[u] > dep[v]) swap(u, v); for (int i = t; i >= 0; --i) if (dep[fa[v][i]] >= dep[u]) v = fa[v][i]; if (u == v) return u; for (int i = t; i >= 0; --i) if (fa[u][i] != fa[v][i]) { u = fa[u][i]; v = fa[v][i]; } if (fa[u][0] != fa[v][0]) return-1; return fa[u][0]; }
inlineintgetlen(int u, int v){ int ans = lca(u, v); if (ans == -1) return-1; return sum[u] + sum[v] - 2 * sum[ans]; }
inlinevoidwork(){ for (int i = 1; i <= n; ++i) {e[i].clear(); sum[i] = dep[i] = fa[n][0] = 0;} t = __lg(n - 1) + 1; for (int i = 1; i <= m; ++i) { int u = rd(), v = rd(), w = rd(); e[u].push_back(make_pair(v, w)); e[v].push_back(make_pair(u, w)); } for (int i = 1; i <= n; ++i) if (dep[i] == 0) { fa[i][0] = i; dfs(i); } for (int i = 1; i <= c; ++i) { int u = rd(), v = rd(); int ans = getlen(u, v); ans == -1 ? puts("Not connected") : printf("%d\n", ans); } }
intmain(){ while (cin >> n >> m >> c) work(); return0; }
#include<bits/stdc++.h> usingnamespacestd; #define N 300007 typedeflonglong ll;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
vector<int> e[N]; int t, fa[N][20], dep[N], d[N], a[N];
inlinevoiddfs(int x){ for (int i = 1; i <= t; ++i) fa[x][i] = fa[fa[x][i - 1]][i - 1]; for (auto j : e[x]) { if(j != fa[x][0]) { fa[j][0] = x; dep[j] = dep[x] + 1; dfs(j); } } }
inlineintlca(int u, int v){ if (dep[u] > dep[v]) swap(u, v); for (int i = t; i >= 0; --i) if (dep[fa[v][i]] >= dep[u]) v = fa[v][i]; if (u == v) return u; for (int i = t; i >= 0; --i) if (fa[u][i] != fa[v][i]) { u = fa[u][i]; v = fa[v][i]; } if (fa[u][0] != fa[v][0]) return-1; return fa[u][0]; }
inlinevoiddfs2(int x){ for (auto i : e[x]) if (i != fa[x][0]) { dfs2(i); d[x] += d[i]; } }
intmain(){ int n = rd(); t = __lg(n - 1) + 1; for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = 1; i <= n - 1; ++i) { int u = rd(), v = rd(); e[u].push_back(v); e[v].push_back(u); } fa[1][0] = 0; dep[1] = 1; dfs(1); for (int i = 1; i < n; ++i) { int ans = lca(a[i], a[i + 1]); d[a[i]]++; d[a[i + 1]]++; d[ans]--; d[fa[ans][0]]--; } dfs2(1); for (int i = 2; i <= n; ++i) --d[a[i]]; for (int i = 1; i <= n; ++i) printf("%d\n", d[i]); return0; }
#include<bits/stdc++.h> usingnamespacestd; #define N 100007 typedeflonglong ll;
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
vector<int> V[N]; int t, fa[N][20], dep[N];
inlinevoiddfs(int x){ for (int i = 1; i <= t; ++i) fa[x][i] = fa[fa[x][i - 1]][i - 1]; for (auto j : V[x]) { if(j != fa[x][0]) { fa[j][0] = x; dep[j] = dep[x] + 1; dfs(j); } } }
inlineintlca(int u, int v){ if (dep[u] > dep[v]) swap(u, v); for (int i = t; i >= 0; --i) if (dep[fa[v][i]] >= dep[u]) v = fa[v][i]; if (u == v) return u; for (int i = t; i >= 0; --i) if (fa[u][i] != fa[v][i]) { u = fa[u][i]; v = fa[v][i]; } if (fa[u][0] != fa[v][0]) return-1; return fa[u][0]; }
inlineintgetlen(int u, int v){ int ans = lca(u, v); if (ans == -1) return-1; return dep[u] + dep[v] - 2 * dep[ans]; }
inlineboolvalid(int m, int l, int r){ return (lca(l, m) == m && dep[m] >= dep[r]); }
inlineboolcheck(int a, int b, int c, int d){ int l1 = lca(a, b), l2 = lca(c, d); return (valid(l2, a, l1) || valid(l2, b, l1) || valid(l1, c, l2) || valid(l1, d, l2)); }
intmain(){ int n = rd(), q = rd(); t = __lg(n - 1) + 1; for (int i = 1; i <= n - 1; ++i) { int u = rd(), v = rd(); V[u].push_back(v); V[v].push_back(u); } for (int i = 1; i <= n; ++i) if (dep[i] == 0) { fa[i][0] = i; dfs(i); } for (int i = 1; i <= q; ++i) { int a = rd(), b = rd(), c = rd(), d = rd(); puts(check(a, b, c, d) ? "Y" : "N"); } return0; }
Post title:树上倍增
Post author:Eva.Q
Create time:2022-02-07 13:22:55
Post link:https://qyy/2022/02/07/Algorithm/multiply on the tree/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.