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;
}
相关文章
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
79 0
The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence
The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence
83 0
The Preliminary Contest for ICPC China Nanchang National Invitational H题 Coloring Game
The Preliminary Contest for ICPC China Nanchang National Invitational H题 Coloring Game
98 0
|
机器学习/深度学习 人工智能
The Preliminary Contest for ICPC China Nanchang National Invitational I题 Max answer
The Preliminary Contest for ICPC China Nanchang National Invitational I题 Max answer
99 0
|
机器学习/深度学习 人工智能
The Preliminary Contest for ICPC China Nanchang National Invitational K题 MORE XOR
The Preliminary Contest for ICPC China Nanchang National Invitational K题 MORE XOR
81 0
|
人工智能
atcoder AtCoder Beginner Contest 210 D - National Railway(dp)
atcoder AtCoder Beginner Contest 210 D - National Railway(dp)
122 0
|
移动开发 前端开发
ICPC Latin American Regional 2017-Imperial roads(LCA)
题目大意: 给出n个点,m条边的一个图,q个询问, 每次询问给出两个点u,v,问包含u-v这条边的最小生成树是多少 这道题比较板 首先求一下这个图的最小生成树对于这n个点,最小生成树一定是n-1条边,如果说再加上一条边,一定会构成一个环。 我们把生成的这个最小生成树看作是一个以1为根节点的最小生成树。 所以说在下面的q个询问中,如果说这条边用到了最小生成树中(这条边是最小生成树上的边),那么直接输出当前最小生成树的代价就好;如果说当前这条边没有出现在最小生成树当中,那么最小生成树的权值val加上这条边之后就构成了一个环,求出这两个点所在的环内的最大边权,并将这个边权减去,就是最终结果
123 0
ICPC Latin American Regional 2017-Imperial roads(LCA)
|
Java
HDU - 2018 Multi-University Training Contest 2 - 1004: Game
HDU - 2018 Multi-University Training Contest 2 - 1004: Game
109 0
|
决策智能
BNUOJ 44578 Monty Hall problem
BNUOJ 44578 Monty Hall problem
121 0
HDOJ 1017 A Mathematical Curiosity
HDOJ 1017 A Mathematical Curiosity
116 0