处理给出无向图,会出现重边,共m条路径,每条路径有一个困难值,q次询问,求从点x出发只经过困难值小于等于v的路径,求某个值。
克鲁斯卡尔重构树的核心思想是,当添加最小生成树的边的时候,不在两个点之间直接加边,而是新建节点,让边的两个节点分别成为它的左右儿子节点,然后这个新建的点,就成为整个联通块的代表点,点权为连边的值(最开始的n个点为点权)。
性质:一个点的所有子树节点的权值都小于等于它的权值,并且从它开始逐渐向子节点移动,权值是单调不上升的。查询时,可以树上倍增得到当前查询点能够到达的最远的祖先点,那么从这个点能够到达的符合边权限制条件的联通块的节点,就是祖先节点的子树中所有的叶节点。
例题:https://cometoj.com/contest/67/problem/D?problem_id=3801
生成克鲁斯卡尔重构树后跑出dfs序,根据dfs序建立线段树计算区间点权之积
#include <bits/stdc++.h>
using namespace std;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
typedef long long ll;
const ll mod = 998244353;
const int maxn = 1e5 + 5;
int n, m, q;
ll a[maxn << 1];
struct Edge
{
int s, e;
bool friend operator <(Edge a, Edge b) {
return max(a.s, a.e) < max(b.s, b.e);
}
} edge[maxn << 1];
int p[maxn << 1];
int value[maxn << 1], cnt;
int find(int x) {
return p[x] == x ? x : p[x] = find(p[x]);
}
vector<int> load[maxn << 1];
int depth[maxn << 1], grand[maxn << 1][20], N;
int in[maxn << 1], out[maxn << 1], ranks[maxn << 1], t;
void dfs(int s, int pre) {
in[s] = ++t;
ranks[t] = s;
depth[s] = depth[pre] + 1;
grand[s][0] = pre;
for (int i = 1; i <= N; ++i)
grand[s][i] = grand[grand[s][i - 1]][i - 1];
for (int i = 0, e; i < load[s].size(); ++i) {
e = load[s][i];
if (e != pre)
dfs(e, s);
}
out[s] = t;
}
void Kruskal() {
cnt = n;
sort(edge + 1, edge + m + 1);
for (int i = 1; i <= n; ++i)
p[i] = i;
for (int i = 1; i <= m; ++i) {
int x = find(edge[i].s);
int y = find(edge[i].e);
if (x == y) continue;
value[++cnt] = max(edge[i].s, edge[i].e);
a[cnt] = 1;
p[cnt] = p[x] = p[y] = cnt;
load[cnt].push_back(x), load[x].push_back(cnt);
load[cnt].push_back(y), load[y].push_back(cnt);
}
N = log2(cnt);
dfs(cnt, 0);
}
ll mul[maxn << 3];
void build(int rt, int l, int r) {
if (l == r) {
mul[rt] = a[ranks[l]];
return;
}
int mid = (l + r) >> 1;
build(lson), build(rson);
mul[rt] = mul[rt << 1] * mul[rt << 1 | 1] % mod;
}
void update(int rt, int l, int r, int pos, int x) {
if (l == r) {
mul[rt] = x;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
update(lson, pos, x);
else
update(rson, pos, x);
mul[rt] = mul[rt << 1] * mul[rt << 1 | 1] % mod;
}
ll query(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R)
return mul[rt];
int mid = (l + r) >> 1;
ll res = 1;
if (L <= mid)
res *= query(lson, L, R), res %= mod;
if (mid < R)
res *= query(rson, L, R), res %= mod;
return res;
}
int main(int argc, char const *argv[])
{
freopen("in.txt", "r", stdin);
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]); a[i] %= mod;
}
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &edge[i].s, &edge[i].e);
}
Kruskal();
build(1, 1, cnt);
int op, x, y;
while (q--) {
scanf("%d%d%d", &op, &x, &y);
if (op == 1) {
if (x > y)
printf("0\n");
else {
for (int i = N; i >= 0; --i) {
if (depth[grand[x][i]] > 0 && value[grand[x][i]] <= y)
x = grand[x][i];
}
printf("%lld\n", query(1, 1, cnt, in[x], out[x]) % mod);
}
}
else
update(1, 1, cnt, in[x], y % mod);
}
}