The Preliminary Contest for ICPC China Nanchang National Invitational J题 Distance on the tree

简介: The Preliminary Contest for ICPC China Nanchang National Invitational J题 Distance on the tree

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≤10

5,1≤m≤10

5

)


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≤10

9

)


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≤10

9

)


Output

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


思路:主席树 + LCA

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int head[maxn], tot;
struct Edge {
  int u, v, w, next;
}edge[maxn];
int root[maxn];
struct Tree {
  int l, r;
  int sum;
}tree[maxn * 40];
int cnt;
int Hash[maxn], len;
int n, m;
int u1[maxn], v1[maxn], w1[maxn];
int u2[maxn], v2[maxn], w2[maxn];
int size[maxn], son[maxn], father[maxn], depth[maxn], top[maxn];
int L[maxn], R[maxn], Index;
void init() {
  memset(head, -1, sizeof(head));
  tot = 1;
}
void add(int u, int v, int w) {
  edge[++tot].u = u; edge[tot].v = v;
  edge[tot].w = w;
  edge[tot].next = head[u];
  head[u] = tot;
}
void updata(int &nowp, int last, int l, int r, int pos) {
    tree[++cnt] = tree[last];
    tree[cnt].sum++; 
    nowp = cnt;
    if(l == r) {
        return ;
    }
    int mid = (l + r) / 2;
    if(pos <= mid) {
        updata(tree[nowp].l, tree[last].l, l, mid, pos);
    } else {
        updata(tree[nowp].r, tree[last].r, mid + 1, r, pos);
    }
}
int query(int lt, int rt, int l, int r, int k) {
    if(l == r) {
        return tree[rt].sum - tree[lt].sum;
    }
    int mid = (l + r) / 2;
    int sum = tree[tree[rt].l].sum - tree[tree[lt].l].sum;
    if(mid >= k) {
        return query(tree[lt].l, tree[rt].l, l, mid, k);
    } else {
        return sum + query(tree[lt].r, tree[rt].r, mid + 1, r, k);
    }
}
void add(int u, int v) {
    edge[++tot].u = u; edge[tot].v = v;
    edge[tot].next = head[u]; 
    head[u] = tot;
}
void dfs1(int u, int fa) {
    size[u] = 1;
    son[u] = 0;
    father[u] = fa;
    depth[u] = depth[fa] + 1;
    int maxson = -1;
    for (int i = head[u]; i != -1; i = edge[i].next) {
        int to = edge[i].v;
        if (to == fa) {
            continue;
        }
        updata(root[to], root[u], 1, len, edge[i].w);
        dfs1(to, u);
        size[u] += size[to];
        if (size[to] > maxson) {
            maxson = size[to];
            son[u] = to;
        }
    }
}
void dfs2(int u, int topf) {
    top[u] = topf;
    L[u] = R[u] = ++Index;
    if (son[u]) {
        dfs2(son[u], topf);
    }
    for (int i = head[u]; i != -1; i = edge[i].next) {
        int to = edge[i].v;
        if (L[to] || to == son[u]) {
            continue;
        }
        dfs2(to, to);
    }
    R[u] = Index;
}
int LCA(int x, int y) {
    while (top[x] != top[y]) {
        if (depth[top[x]] < depth[top[y]]) {
            swap(x, y);
        }
        x = father[top[x]];
    }
    if (depth[x] > depth[y]) {
        return y;
    }
    return x;
}
int main() {
  scanf("%d%d", &n, &m);
  init();
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d%d", &u1[i], &v1[i], &w1[i]);
    Hash[++len] = w1[i];
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &u2[i], &v2[i], &w2[i]);
    Hash[++len] = w2[i];
  }
  sort(Hash + 1, Hash + len + 1);
  len = unique(Hash + 1, Hash + len + 1) - Hash - 1;
  for (int i = 1; i <= n - 1; i++) {
    w1[i] = lower_bound(Hash + 1, Hash + len + 1, w1[i]) - Hash;
    add(u1[i], v1[i], w1[i]);
    add(v1[i], u1[i], w1[i]);
  }
  for (int i = 1; i <= m; i++) {
    w2[i] = lower_bound(Hash + 1, Hash + len + 1, w2[i]) - Hash;
  }
  dfs1(1, 0);
  dfs2(1, 1);
  for (int i = 1; i <= m; i++) {
    int L1 = LCA(u2[i], v2[i]);
    int x = query(root[L1], root[u2[i]], 1, len, w2[i]);
    int y = query(root[L1], root[v2[i]], 1, len, w2[i]);
    printf("%d\n",  x + y);
  }
  return 0;
}
相关文章
|
6月前
Leetcode 365. Water and Jug Problem
一句话理解题意:有容积为x和y升的俩水壶,能不能量出z升的水。 我刚开始看到这题,立马就想了下暴力搜索的可能性,但考虑了下数据大小,立马放弃这个暴力的想法,于是意识到肯定有比较简单的数学方法,其实我自己没想到,后来看还是看了别人的代码,很多博客都直接给出了解法, 但没介绍为什么能这么解。所以我决定解释下我自己的思路。
26 0
|
12月前
|
机器学习/深度学习 人工智能
The Preliminary Contest for ICPC China Nanchang National Invitational I题 Max answer
The Preliminary Contest for ICPC China Nanchang National Invitational I题 Max answer
75 0
|
12月前
The Preliminary Contest for ICPC China Nanchang National Invitational A题 PERFECT NUMBER PROBLEM
The Preliminary Contest for ICPC China Nanchang National Invitational A题 PERFECT NUMBER PROBLEM
50 0
|
12月前
The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence
The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence
51 0
|
12月前
The Preliminary Contest for ICPC China Nanchang National Invitational H题 Coloring Game
The Preliminary Contest for ICPC China Nanchang National Invitational H题 Coloring Game
69 0
|
12月前
|
机器学习/深度学习 人工智能
The Preliminary Contest for ICPC China Nanchang National Invitational K题 MORE XOR
The Preliminary Contest for ICPC China Nanchang National Invitational K题 MORE XOR
53 0
LeetCode 365. Water and Jug Problem
有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?
60 0
LeetCode 365. Water and Jug Problem
|
人工智能
atcoder AtCoder Beginner Contest 210 D - National Railway(dp)
atcoder AtCoder Beginner Contest 210 D - National Railway(dp)
85 0
The 15th Chinese Northeast Collegiate Programming Contest C. Vertex Deletion(树形dp)
The 15th Chinese Northeast Collegiate Programming Contest C. Vertex Deletion(树形dp)
86 0