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的条数
利用树链剖分+离线线段树进行操作
复杂度为NlogN
#include<bits/stdc++.h>
#define l(x) Tree[x].l
#define r(x) Tree[x].r
#define sum(x) Tree[x].sum
#define ls(x) x << 1
#define rs(x) x << 1 | 1
;
], next[MAXN << ], head[MAXN], tot;
int fa[MAXN], son[MAXN], siz[MAXN], dep[MAXN];
int top[MAXN], tid[MAXN], rnk[MAXN], pos;
int eid[MAXN];
struct segmentT {
int l, r;
int sum;
} Tree[MAXN << ];
void build(int p, int l, int r) {
l(p) = l, r(p) = r;
if (l == r) return;
;
build(ls(p), l, mid);
build(rs(p), mid + , r);
}
void change(int p, int x) {
if (l(p) == r(p)) {
sum(p) = ;
return;
}
;
if (x <= mid)
change(ls(p), x);
else
change(rs(p), x);
sum(p) = sum(ls(p)) + sum(rs(p));
}
int ask(int p, int l, int r) {
if (l <= l(p) && r(p) <= r) return sum(p);
;
;
if (l <= mid) val += ask(ls(p), l, r);
if (r > mid) val += ask(rs(p), l, r);
return val;
}
void add(int u, int v) {
++tot, ver[tot] = v, next[tot] = head[u], head[u] = tot;
}
int dfs1(int u, int f) {
dep[u] = dep[f] + , siz[u] = , son[u] = , fa[u] = f;
for (int i = head[u]; i; i = next[i]) {
int v = ver[i];
if (v == f) continue;
siz[u] += dfs1(v, u);
eid[(i-) / + ] = v;
if (siz[v] > siz[son[u]]) son[u] = v;
}
return siz[u];
}
void dfs2(int u, int tp) {
top[u] = tp, tid[u] = ++pos, rnk[pos] = u;
if (!son[u]) return;
dfs2(son[u], tp);
for (int i = head[u]; i; i = next[i]) {
int v = ver[i];
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
int linkquery(int u, int v) {
;
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
ans += ask(, tid[top[u]], tid[u]);
u = fa[top[u]];
}
if (u == v) return ans;
if (tid[v] < tid[u]) std::swap(u, v);
ans += ask(, tid[u] + , tid[v]);
return ans;
}
struct node {
int u, v, w, id;
bool operator<(const node& a) const{
return w < a.w;
}
} q[MAXN], p[MAXN];
int ans[MAXN];
int main() {
int n, m;
scanf("%d%d", &n, &m);
; i < n; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u, v), add(v, u);
p[i].u = u, p[i].v = v, p[i].w = w, p[i].id = i;
}
; i <= m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
q[i].u = u, q[i].v = v, q[i].w = w, q[i].id = i;
}
std::sort(p + , p + n);
std::sort(q + , q + m + );
dfs1(, );
dfs2(, );
build(, , n);
int j;
; i <= m; i++) {
while (j < n && p[j].w <= q[i].w) {
change(, tid[eid[p[j].id]]), j++;
}
ans[q[i].id] = linkquery(q[i].u, q[i].v);
}
; i <= m; i++) {
printf("%d\n", ans[i]);
}
;
}
2019南昌邀请赛网络赛:J distance on the tree的更多相关文章
- 计蒜客 2019南昌邀请网络赛J Distance on the tree(主席树)题解
题意:给出一棵树,给出每条边的权值,现在给出m个询问,要你每次输出u~v的最短路径中,边权 <= k 的边有几条 思路:当时网络赛的时候没学过主席树,现在补上.先树上建主席树,然后把边权交给子节 ...
- 2019南昌邀请赛网络预选赛 J.Distance on the tree(树链剖分)
传送门 题意: 给出一棵树,每条边都有权值: 给出 m 次询问,每次询问有三个参数 u,v,w ,求节点 u 与节点 v 之间权值 ≤ w 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说, ...
- POJ-2796 & 2019南昌邀请赛网络赛 I. 区间最大min*sum
http://poj.org/problem?id=2796 https://nanti.jisuanke.com/t/38228 背景 给定一个序列,对于任意区间,min表示区间中最小的数,sum表 ...
- 南昌网络赛J. Distance on the tree 树链剖分+主席树
Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...
- 南昌网络赛J. Distance on the tree 树链剖分
Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...
- 2019南昌网络赛 J Distance on the tree 主席树+lca
题意 给一颗树,每条边有边权,每次询问\(u\)到\(v\)的路径中有多少边的边权小于等于\(k\) 分析 在树的每个点上建\(1\)到\(i\)的权值线段树,查询的时候同时跑\(u,v,lca ...
- 2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树
边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<st ...
- [2019南昌邀请赛网络赛D][dp]
https://nanti.jisuanke.com/t/38223 Xiao Ming recently indulges in match stick game and he thinks he ...
- 南昌邀请赛网络赛 D.Match Stick Game(dp)
南昌邀请赛网络赛 D.Match Stick Game 题目传送门 题目就会给你一个长度为n的字符串,其中\(1<n<100\).这个字符串是一个表达式,只有加减运算符,然后输入的每一个字 ...
随机推荐
- python中的yield关键字
yield关键字一直困扰了我很久,一直也没有弄明白,现在将暂时理解的yield记录如下,供参考: 关键词:可迭代对象,生成器,迭代器 一.可迭代对象: 可迭代对象:可迭代对象是一个泛称,只要可以用fo ...
- Struts2 - 配置文件中result 节点详解
每个 action 方法都将返回一个 String 类型的值, Struts 将根据这个值来决定响应什么结果. 每个 action 声明都必须包含有数量足够多的 result 元素, 每个 resul ...
- windows下用vs2010编译ffmpeg
转载自;http://q1q2q3q4q5q6ln.blog.163.com/blog/static/500794332014666536283/ (注意:请务必先阅读:七,后记补充:) ffmpeg ...
- 2017-2018-1 20179215《Linux内核原理与分析》第四周作业
本次的实验是使用gdb跟踪调试内核从start_kernel到init进程启动,并分析启动的过程. 1.首先是在实验楼虚拟机上进行调试跟踪的过程. cd LinuxKernel qemu -kerne ...
- [BALTIC 2008] Grid
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1169 [算法] 首先DFS枚举出横着切的 然后二分 + 贪心即可 时间复杂度 : O ...
- NOIP2018爆炸记
又是一年\(NOIP\),可能是梦结束的地方? 之所以咕了这么久是得先确定自己不会退役,因为分太低了. 和去年一样在学校门前照了相,然后上车走了.高三回来考的只剩下\(p2oileen\)学姐了.新一 ...
- 洛谷【P1898】缘分计算
我对模拟的理解:http://www.cnblogs.com/AKMer/p/9064018.html 题目传送门:https://www.luogu.org/problemnew/show/P189 ...
- spring IOC 注解@Autowired
自动装配:按照类型来找 会在xml中找类型一样的, 比如 setMessage(SetName setName)上面有@Autowired,会到xml中找<bean id="setna ...
- Python-socket发送文件并解决粘包问题
服务器端要先根据客户端要下载的文件进行判断是否存在,还要根据文件大小来进行传送,最后还要比对文件的md5值来判断传送的文件是否正确,通过判断剩余字节来解决粘包问题 服务器端 # -*- coding: ...
- C#对SQL数据库操作类简介:Connection、Command、DataReader、DataSet、DataAdapter
在说C#操作数据库之前需要先说下ADO.NET.ADO.NET的名称起源于ADO(ActiveX Data Objects),是一个COM组件库,用于在以往的Microsoft技术中访问数据.之所以使 ...