红蓝图
给定两个参数x,tx, tx,t,删除边权大于ttt的红边,和边权小于ttt的蓝边,问对于所有的点yyy,既能通过红边走向xxx,又能通过蓝边走向xxx,的点有多少个。
考虑对红边按照边权升序建立一颗kruskalkruskalkruskal重构树,对蓝边按照边权降序建立一颗kruskalkruskalkruskal重构树,
在树一中我们向上跳,找到点权小于等于ttt的深度最浅的点uuu,同理在树二上我们向上跳,找到点权大于等于ttt的深度最浅的点vvv,
最后我们只需要判断,这两个点所代表的子树集合交集的大小即是答案。
最后要注意,题目中没有说明这是一个连通图,一意味着,可能有好几颗树!!!
#include <bits/stdc++.h>using namespace std;const int N = 4e5 + 10;struct Res {int u, v, w, op;bool operator < (const Res &t) const {return w < t.w;}
}edge[N];int n, m, Q, x[N], y[N];int root[N], ls[N << 5], rs[N<< 5], sum[N << 5], num;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 rt1, int rt2, int l, int r, int L, int R) {if (l >= L && r <= R) {return sum[rt2] - sum[rt1];}int ans = 0, mid = l + r >> 1;if (L <= mid) {ans += query(ls[rt1], ls[rt2], l, mid, L, R);}if (R > mid) {ans += query(rs[rt1], rs[rt2], mid + 1, r, L, R);}return ans;
}struct Kruskal {int head[N], to[N], nex[N], cnt = 1;int ff[N], value[N], fa[N][21], l[N], r[N], rk[N], tot, nn;void add(int x, int y) {to[cnt] = y;nex[cnt] = head[x];head[x] = cnt++;}int find(int rt) {return ff[rt] == 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(int rev) {if (rev) {reverse(edge + 1, edge + 1 + m);}for (int i = 1; i < N; i++) {ff[i] = i;}nn = n;for (int i = 1, cur = 1; i <= m && cur < n; i++) {if (edge[i].op != rev) {continue;}int u = find(edge[i].u), v = find(edge[i].v);if (u != v) {cur++, nn++;ff[u] = 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;}}}for (int i = 1; i < 2 * n; i++) {if (ff[i] == i) {dfs(i, 0);}}}
}a, b;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, u, v, op; i <= m; i++) {scanf("%d %d %d", &u, &v, &op);u++, v++;edge[i] = {u, v, i, op};}sort(edge + 1, edge + 1 + m);a.kruskal(0);b.kruskal(1);for (int i = 1; i < 2 * n; i++) {x[i] = a.rk[i];y[i] = b.l[i];}for (int i = 1; i < 2 * n; i++) {root[i] = root[i - 1];if (x[i] <= n) {update(root[i], root[i - 1], 1, 2 * n, y[x[i]], 1);}}int x, t;while (Q--) {scanf("%d %d", &x, &t);int u = x + 1, v = x + 1;for (int i = 20; i >= 0; i--) {if (a.fa[u][i] && a.value[a.fa[u][i]] <= t) {u = a.fa[u][i];}}for (int i = 20; i >= 0; i--) {if (b.fa[v][i] && b.value[b.fa[v][i]] >= t) {v = b.fa[v][i];}}int l1 = a.l[u], r1 = a.r[u], l2 = b.l[v], r2 = b.r[v];printf("%d\n", query(root[l1 - 1], root[r1], 1, 2 * n, l2, r2));}return 0;
}