kruskal重构树
如何建树
模仿kruskalkruskalkruskal,先将所有边排序。
依次遍历每一条边,如果这条边的两个节点(u,vu, vu,v)不在同一个连通块里面,
则新建一个nodenodenode节点,更新fa[u]=fa[v]=nodefa[u] = fa[v] = nodefa[u]=fa[v]=node,同时有fa[node]=nodefa[node] = nodefa[node]=node,
建立node−>u,node−>vnode-> u, node-> vnode−>u,node−>v的边,同时给nodenodenode节点赋值上这条边的边权,
一些性质
① 是一个小/大根堆(由建树时边权的排序方式决定)。
② LCA(u,v)LCA(u, v)LCA(u,v)的权值是从uuu到vvv的路径上的最大/最小边权的最小/最大值(同样由建树是边权的排序所决定)。
显然,对于每个新建的nodenodenode节点,一定有权值是由深度更小到深度更大递减的(如果排序时按照升序排列),反之亦然,所以易证得 ① ② 成立。
#3732. Network
#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;int value[N], ff[N], nn, n, m, q;int head[N], to[N], nex[N], cnt = 1;int fa[N], son[N], sz[N], dep[N], top[N], tot;struct Res {int u, v, w;void read() {scanf("%d %d %d", &u, &v, &w);}bool operator < (const Res &t) const {return w < t.w;}
}a[N];void add(int x, int y) {to[cnt] = y;nex[cnt] = head[x];head[x] = cnt++;
}int find(int rt) {return rt == ff[rt] ? rt : ff[rt] = find(ff[rt]);
}void dfs1(int rt, int f) {fa[rt] = f, dep[rt] = dep[f] + 1, sz[rt] = 1;for (int i = head[rt]; i; i = nex[i]) {if (to[i] == f) {continue;}dfs1(to[i], rt);sz[rt] += sz[to[i]];if (sz[son[rt]] < sz[to[i]]) {son[rt] = to[i];}}
}void dfs2(int rt, int tp) {top[rt] = tp;if (!son[rt]) {return ;}dfs2(son[rt], tp);for (int i = head[rt]; i; i = nex[i]) {if (to[i] == fa[rt] || to[i] == son[rt]) {continue;}dfs2(to[i], to[i]);}
}int lca(int x, int y) {while (top[x] != top[y]) {if (dep[top[x]] < dep[top[y]]) {swap(x, y);}x = fa[top[x]];}return dep[x] < dep[y] ? x : y;
}void kruskal() {for (int i = 1; i < N; i++) {ff[i] = i;}for (int i = 1; i <= m; i++) {int u = a[i].u, v = a[i].v;int fu = find(u), fv = find(v);if (fu != fv) {value[++n] = a[i].w;ff[n] = ff[fu] = ff[fv] = n;add(n, fv), add(n, fu);}}dfs1(n, 0), dfs2(n, n);
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d %d", &nn, &m, &q), n = nn;for (int i = 1; i <= m; i++) {a[i].read();}sort(a + 1, a + 1 + m);kruskal();while (q--) {int u, v;scanf("%d %d", &u, &v);printf("%d\n", value[lca(u, v)]);}return 0;
}
#3551. [ONTAK2010]Peaks加强版
我们要求从一个点出发经过困难值小于等于xxx的路径所能到达的山峰中第kkk高的是什么。
考虑按照边权升序,建议kruskalkruskalkruskal重构树,然后倍增向上跳,找到困难值小于等于xxx的深度最小的节点uuu,
那么我们只要在uuu的子树中询问第kkk大即可,所以可以用主席树来写,依照dfsdfsdfs序,对每个节点建立一颗主席树,然后在主席树上查找第kkk大即可。
#include <bits/stdc++.h>using namespace std;const int N = 5e5 + 10;int head[N], to[N], nex[N], cnt = 1;int n, m, q, nn, a[N], ff[N], value[N], h[N];int fa[N][21], l[N], r[N], rk[N], tot;int root[N], ls[N << 7], rs[N << 7], sum[N << 7], num;struct Res {int u, v, w;bool operator < (const Res &t) const {return w < t.w;}
}edge[N];void add(int x, int y) {to[cnt] = y;nex[cnt] = head[x];head[x] = cnt++;
}void update(int &rt, int pre, int l, int r, int x, int v) {rt = ++num;ls[rt] = ls[pre], rs[rt] = rs[pre], sum[rt] = sum[pre] + v;if (l == r) {return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], ls[pre], l, mid, x, v);}else {update(rs[rt], rs[pre], mid + 1, r, x, v);}
}int query(int L, int R, int l, int r, int k) {if (l == r) {return l;}int res = sum[ls[R]] - sum[ls[L]], mid = l + r >> 1;if (res >= k) {return query(ls[L], ls[R], l, mid, k);}else {return query(rs[L], rs[R], mid + 1, r, k - res);}
}int find(int rt) {return rt == ff[rt] ? rt : ff[rt] = find(ff[rt]);
}void dfs(int rt, int f) {fa[rt][0] = f, l[rt] = ++tot, rk[tot] = rt;for (int i = 1; i <= 20; i++) {fa[rt][i] = fa[fa[rt][i - 1]][i - 1];}for (int i = head[rt]; i; i = nex[i]) {if (to[i] == f) {continue;}dfs(to[i], rt);}r[rt] = tot;
}void kruskal() {for (int i = 1; i < N; i++) {ff[i] = i;}sort(edge + 1, edge + 1 + m);for (int i = 1, cur = 1; i <= m && cur < n; i++) {int u = find(edge[i].u), v = find(edge[i].v);if (u != v) {cur++, nn++;ff[u] = nn, ff[v] = nn;value[nn] = edge[i].w;add(nn, u), add(nn, v);if (u <= n) {value[u] = edge[i].w;}if (v <= n) {value[v] = edge[i].w;}}}dfs(nn, 0);
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d %d", &n, &m, &q);for (int i = 1; i <= n; i++) {scanf("%d", &h[i]);a[i] = h[i];}nn = n;for (int i = 1; i <= m; i++) {scanf("%d %d %d", &edge[i].u, &edge[i].v, &edge[i].w);}kruskal();int maxn = n;sort(a + 1, a + 1 + maxn);maxn = unique(a + 1, a + 1 + maxn) - (a + 1);for (int i = 1; i <= n; i++) {h[i] = lower_bound(a + 1, a + 1 + maxn, h[i]) - a; }for (int i = 1; i <= nn; i++) {root[i] = root[i - 1];if (rk[i] <= n) {update(root[i], root[i], 1, maxn, h[rk[i]], 1);}}for (int i = 1, u, x, k, last_ans = 0, res; i <= q; i++) {scanf("%d %d %d", &u, &x, &k);if (last_ans != -1) {u ^= last_ans, x ^= last_ans, k ^= last_ans;}for (int j = 20; j >= 0; j--) {if (fa[u][j] && value[fa[u][j]] <= x) {u = fa[u][j];}}res = sum[root[r[u]]] - sum[root[l[u] - 1]];last_ans = res < k ? -1 : a[query(root[l[u] - 1], root[r[u]], 1, maxn, res - k + 1)];printf("%d\n", last_ans);}return 0;
}
P4768 [NOI2018] 归程
给定一个nnn个点,mmm条边的无向联通图,边的描述为[u,v,l,a][u, v, l, a][u,v,l,a],表示uuu,vvv连有一条长度为lll,海拔为aaa的边,
有QQQ个询问,每次给出一个出发点uuu和一个海拔限制高度ppp,并且在出发点有一辆车,这辆车可以通过海拔大于ppp的边,
问,从u−>1u->1u−>1的最短步行长度是什么多少。
设从uuu坐车出发可到的点集为SSS,我们的任务就是找到一个点v,v∈Sv, v \in Sv,v∈S,dis(v,1)dis(v, 1)dis(v,1)是dis(x,1),x∈Sdis(x, 1),x \in Sdis(x,1),x∈S中最的小。
① 预处理出每个点到点111的最短路径出来,
② 我们按照海拔高度降序建立一颗kruskalkruskalkruskal重构树,
③ 从uuu号点往上跳,找到可坐车到达的深度最小的节点rtrtrt,显然从uuu可坐车到达的点集就是rtrtrt所在的这颗子树,
④ 由于我们查找的是最小值,所以只需在dfsdfsdfs的过程中,不断向上更新整颗子树的最小值即可。
⑤ 直接输出我们找到的rtrtrt所代表的答案。
#include <bits/stdc++.h>using namespace std;const int N = 1e6 + 10;int head[N], to[N], nex[N], cnt = 1;int head1[N], to1[N], nex1[N], value1[N], cnt1 = 1;int vis[N], dis[N], ff[N], value[N], fa[N][21], ans[N], nn, n, m, Q, K, S;struct Edge {int u, v, w;bool operator < (const Edge &t) const {return w > t.w;}
}edge[N];struct Node {int u, w;bool operator < (const Node &t) const {return w > t.w;}
};void add1(int x, int y, int w) {to1[cnt1] = y;nex1[cnt1] = head1[x];value1[cnt1] = w;head1[x] = cnt1++;
}void add(int x, int y) {to[cnt] = y;nex[cnt] = head[x];head[x] = cnt++;
}priority_queue<Node> q;void Dijkstra() {while (q.size()) {q.pop();}q.push({1, 0});memset(vis, 0, sizeof vis), memset(dis, 0x3f, sizeof dis);dis[1] = 0;while (q.size()) {int rt = q.top().u;q.pop();if (vis[rt]) {continue;}vis[rt] = 1;for (int i = head1[rt]; i; i = nex1[i]) {if (dis[to1[i]] > dis[rt] + value1[i]) {dis[to1[i]] = dis[rt] + value1[i];q.push({to1[i], dis[to1[i]]});}}}
}int find(int rt) {return ff[rt] == rt ? rt : ff[rt] = find(ff[rt]);
}void dfs(int rt, int f) {fa[rt][0] = f, ans[rt] = rt <= n ? dis[rt] : 0x3f3f3f3f;for (int i = 1; i <= 20; i++) {fa[rt][i] = fa[fa[rt][i - 1]][i - 1];}for (int i = head[rt]; i; i = nex[i]) {if (to[i] == f) {continue;}dfs(to[i], rt);ans[rt] = min(ans[rt], ans[to[i]]);}
}void kruskal() {for (int i = 1; i < N; i++) {ff[i] = i, head[i] = 0;}cnt = 1;sort(edge + 1, edge + 1 + m);for (int i = 1, cur = 1; i <= m && cur < n; i++) {int u = find(edge[i].u), v = find(edge[i].v);if (u != v) {cur++, nn++;ff[u] = nn, ff[v] = nn;value[nn] = edge[i].w;add(nn, u), add(nn, v);if (u <= n) {value[u] = edge[i].w;}if (v <= n) {value[v] = edge[i].w;}}}dfs(nn, 0);
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int T;scanf("%d", &T);while (T--) {scanf("%d %d", &n, &m);nn = n;memset(head1, 0, sizeof head1), cnt1 = 1;for (int i = 1, u, v, l, a; i <= m; i++) {scanf("%d %d %d %d", &u, &v, &l, &a);add1(u, v, l), add1(v, u, l);edge[i] = {u, v, a};}Dijkstra();kruskal();scanf("%d %d %d", &Q, &K, &S);for (int i = 1, v, p, last_ans = 0; i <= Q; i++) {scanf("%d %d", &v, &p);v = (v + 1ll * K * last_ans - 1) % n + 1, p = (p + 1ll * K * last_ans) % (S + 1);for (int j = 20; j >= 0; j--) {if (fa[v][j] && value[fa[v][j]] > p) {v = fa[v][j];}}last_ans = ans[v];printf("%d\n", last_ans);}}return 0;
}