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;
}
相关文章
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8067 2
|
11天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18469 64
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
9天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1500 5
|
8天前
|
云安全 数据采集 人工智能
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2641 8
|
8天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2115 0