2019南昌邀请赛网络赛:J distance on the tree

  •  1000ms
  •  262144K
 

DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(National Olympiad in Informatics in Provinces) in Senior High School. So when in Data Structure Class in College, he is always absent-minded about what the teacher says.

The experienced and knowledgeable teacher had known about him even before the first class. However, she didn't wish an informatics genius would destroy himself with idleness. After she knew that he was so interested in ACM(ACM International Collegiate Programming Contest), she finally made a plan to teach him to work hard in class, for knowledge is infinite.

This day, the teacher teaches about trees." A tree with nn nodes, can be defined as a graph with only one connected component and no cycle. So it has exactly n-1n−1 edges..." DSM is nearly asleep until he is questioned by teacher. " I have known you are called Data Structure Master in Graph Theory, so here is a problem. "" A tree with nn nodes, which is numbered from 11 to nn. Edge between each two adjacent vertexes uu and vv has a value w, you're asked to answer the number of edge whose value is no more than kk during the path between uu and vv."" If you can't solve the problem during the break, we will call you DaShaMao(Foolish Idiot) later on."

The problem seems quite easy for DSM. However, it can hardly be solved in a break. It's such a disgrace if DSM can't solve the problem. So during the break, he telephones you just for help. Can you save him for his dignity?

Input

In the first line there are two integers n,mn,m, represent the number of vertexes on the tree and queries(2 \le n \le 10^5,1 \le m \le 10^52≤n≤105,1≤m≤105)

The next n-1n−1 lines, each line contains three integers u,v,wu,v,w, indicates there is an undirected edge between nodes uu and vv with value ww. (1 \le u,v \le n,1 \le w \le 10^91≤u,v≤n,1≤w≤109)

The next mm lines, each line contains three integers u,v,ku,v,k , be consistent with the problem given by the teacher above. (1 \le u,v \le n,0 \le k \le 10^9)(1≤u,v≤n,0≤k≤109)

Output

For each query, just print a single line contains the number of edges which meet the condition.

样例输入1

3 3

1 3 2
2 3 7
1 3 0
1 2 4
1 2 7

样例输出1

0

1
2

样例输入2

5 2

1 2 1000000000
1 3 1000000000
2 4 1000000000
3 5 1000000000
2 3 1000000000
4 5 1000000000

样例输出2

2

4

题意简述:给定一棵树,询问m次,求u->v树上路径权值≤k的条数

利用树链剖分+离线线段树进行操作

扫描二维码关注公众号,回复: 6088769 查看本文章

复杂度为NlogN

  1 #include<bits/stdc++.h>
  2 
  3 #define l(x) Tree[x].l
  4 #define r(x) Tree[x].r
  5 #define sum(x) Tree[x].sum
  6 #define ls(x) x << 1
  7 #define rs(x) x << 1 | 1
  8 
  9 const int MAXN = (int)1e5 + 5;
 10 
 11 int ver[MAXN << 1], next[MAXN << 1], head[MAXN], tot;
 12 int fa[MAXN], son[MAXN], siz[MAXN], dep[MAXN];
 13 int top[MAXN], tid[MAXN], rnk[MAXN], pos;
 14 int eid[MAXN];
 15 
 16 struct segmentT {
 17     int l, r;
 18     int sum;
 19 } Tree[MAXN << 2];
 20 
 21 void build(int p, int l, int r) {
 22     l(p) = l, r(p) = r;
 23     if (l == r) return;
 24     int mid = (l + r) / 2;
 25     build(ls(p), l, mid);
 26     build(rs(p), mid + 1, r);
 27 }
 28 
 29 void change(int p, int x) {
 30     if (l(p) == r(p)) {
 31         sum(p) = 1;
 32         return;
 33     }
 34     int mid = (l(p) + r(p)) / 2;
 35     if (x <= mid)
 36         change(ls(p), x);
 37     else
 38         change(rs(p), x);
 39     sum(p) = sum(ls(p)) + sum(rs(p));
 40 }
 41 
 42 int ask(int p, int l, int r) {
 43     if (l <= l(p) && r(p) <= r) return sum(p);
 44     int mid = (l(p) + r(p)) / 2;
 45     int val = 0;
 46     if (l <= mid) val += ask(ls(p), l, r);
 47     if (r > mid) val += ask(rs(p), l, r);
 48     return val;
 49 }
 50 
 51 void add(int u, int v) {
 52     ++tot, ver[tot] = v, next[tot] = head[u], head[u] = tot;
 53 }
 54 
 55 int dfs1(int u, int f) {
 56     dep[u] = dep[f] + 1, siz[u] = 1, son[u] = 0, fa[u] = f;
 57     for (int i = head[u]; i; i = next[i]) {
 58         int v = ver[i];
 59         if (v == f) continue;
 60         siz[u] += dfs1(v, u);
 61         eid[(i-1) / 2 + 1] = v;
 62         if (siz[v] > siz[son[u]]) son[u] = v;
 63     }
 64     return siz[u];
 65 }
 66 
 67 void dfs2(int u, int tp) {
 68     top[u] = tp, tid[u] = ++pos, rnk[pos] = u;
 69     if (!son[u]) return;
 70     dfs2(son[u], tp);
 71     for (int i = head[u]; i; i = next[i]) {
 72         int v = ver[i];
 73         if (v == fa[u] || v == son[u]) continue;
 74         dfs2(v, v);
 75     }
 76 }
 77 
 78 int linkquery(int u, int v) {
 79     int ans = 0;
 80     while (top[u] != top[v]) {
 81         if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
 82         ans += ask(1, tid[top[u]], tid[u]);
 83         u = fa[top[u]];
 84     }
 85     if (u == v) return ans;
 86     if (tid[v] < tid[u]) std::swap(u, v);
 87     ans += ask(1, tid[u] + 1, tid[v]);
 88     return ans;
 89 }
 90 
 91 struct node {
 92     int u, v, w, id;
 93     bool operator<(const node& a) const{
 94         return w < a.w;
 95     }
 96 } q[MAXN], p[MAXN];
 97 
 98 int ans[MAXN];
 99 
100 int main() {
101     int n, m;
102     scanf("%d%d", &n, &m);
103     for (int i = 1; i < n; i++) {
104         int u, v, w;
105         scanf("%d%d%d", &u, &v, &w);
106         add(u, v), add(v, u);
107         p[i].u = u, p[i].v = v, p[i].w = w, p[i].id = i;
108     }
109     for (int i = 1; i <= m; i++) {
110         int u, v, w;
111         scanf("%d%d%d", &u, &v, &w);
112         q[i].u = u, q[i].v = v, q[i].w = w, q[i].id = i;
113     }
114     std::sort(p + 1, p + n);
115     std::sort(q + 1, q + m + 1);
116 
117     dfs1(1, 0);
118     dfs2(1, 1);
119     build(1, 1, n);
120 
121     int j;
122     for (int i = 1; i <= m; i++) {
123         while (j < n && p[j].w <= q[i].w) {
124             change(1, tid[eid[p[j].id]]), j++;
125         }
126         ans[q[i].id] = linkquery(q[i].u, q[i].v);
127     }
128     for (int i = 1; i <= m; i++) {
129         printf("%d\n", ans[i]);
130     }
131     return 0;
132 }

猜你喜欢

转载自www.cnblogs.com/rign/p/10800640.html