SPOJ 375 树链剖分 QTREE - Query on a tree
人生第一道树链剖分的题目,其实树链剖分并不是特别难。
思想就是把树剖成一些轻链和重链,轻链比较少可以直接修改,重链比较长,用线段树去维护。
貌似大家都是从这篇博客上学的。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = + ; int n;
int tot;
vector<int> G[maxn];
int u[maxn], v[maxn], d[maxn]; int fa[maxn];
int top[maxn];
int id[maxn];
int L[maxn];
int son[maxn];
int sz[maxn]; void dfs(int u)
{
sz[u] = ; son[u] = ;
for(int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if(v == fa[u]) continue;
fa[v] = u;
L[v] = L[u] + ;
dfs(v);
sz[u] += sz[v];
if(sz[v] > sz[son[u]]) son[u] = v;
}
} void dfs2(int u, int tp)
{
id[u] = ++tot;
top[u] = tp;
if(son[u]) dfs2(son[u], tp);
for(int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if(v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
} int maxv[maxn << ]; void update(int o, int L, int R, int p, int val)
{
if(p < L || p > R) return ;
if(L == R) { maxv[o] = val; return ; }
int M = (L + R) / ;
update(o<<, L, M, p, val);
update(o<<|, M+, R, p, val);
maxv[o] = max(maxv[o<<], maxv[o<<|]);
} int query(int o, int L, int R, int qL, int qR)
{
if(qR < L || qL > R) return ;
if(qL <= L && R <= qR) return maxv[o];
int M = (L + R) / ;
return max(query(o<<, L, M, qL, qR), query(o<<|, M+, R, qL, qR));
} int QUERY(int u, int v)
{
int ans = ;
int t1 = top[u], t2 = top[v];
while(t1 != t2)
{
if(L[t1] < L[t2]) { swap(u, v); swap(t1, t2); }
ans = max(ans, query(, , tot, id[t1], id[u]));
u = fa[t1]; t1 = top[u];
}
if(u == v) return ans;
if(L[u] < L[v]) swap(u, v);
ans = max(ans, query(, , tot, id[son[v]], id[u]));
return ans;
} int main()
{
int T; scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i <= n; i++) G[i].clear();
fa[] = L[] = ;
for(int i = ; i < n; i++)
{
scanf("%d%d%d", u + i, v + i, d + i);
G[u[i]].push_back(v[i]);
G[v[i]].push_back(u[i]);
} dfs();
tot = ;
dfs2(, ); memset(maxv, , sizeof(maxv));
for(int i = ; i < n; i++)
{
if(L[u[i]] < L[v[i]]) swap(u[i], v[i]);
update(, , tot, id[u[i]], d[i]);
} char op[];
while(scanf("%s", op) && op[] != 'D')
{
int x, y; scanf("%d%d", &x, &y);
if(op[] == 'Q')
{
printf("%d\n", QUERY(x, y));
}
else
{
update(, , tot, id[u[x]], y);
}
}
} return ;
}
代码君
SPOJ 375 树链剖分 QTREE - Query on a tree的更多相关文章
- SPOJ 375 树链剖分
SPOJ太慢了,SPOJ太慢了, 题意:给定n(n<=10000)个节点的树,每条边有边权,有两种操作:1.修改某条变的边权:2.查询u,v之间路径上的最大边权. 分析:树链剖分入门题,看这里: ...
- SPOJ 375 (树链剖分 - 边权剖分 - 修改单边权)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/I 给你一棵有边权的树,有两个操作:一个操作是输出l到 ...
- SPOJ 375 (树链剖分+线段树)
题意:一棵包含N 个结点的树,每条边都有一个权值,要求模拟两种操作:(1)改变某条边的权值,(2)询问U,V 之间的路径中权值最大的边. 思路:最近比赛总是看到有树链剖分的题目,就看了论文,做了这题, ...
- spoj 375 树链剖分 模板
QTREE - Query on a tree #tree You are given a tree (an acyclic undirected connected graph) with N no ...
- spoj 375 树链剖分模板
/* 只是一道树链刨分的入门题,作为模板用. */ #include<stdio.h> #include<string.h> #include<iostream> ...
- 数据结构(并查集||树链剖分):HEOI 2016 tree
[注意事项] 为了体现增强版,题目限制和数据范围有所增强: 时间限制:1.5s 内存限制:128MB 对于15% 的数据,1<=N,Q<=1000. 对于35% 的数据,1<=N,Q ...
- 树链剖分【CF343D】Water Tree
Description Mad scientist Mike has constructed a rooted tree, which consists of nnvertices. Each ver ...
- bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 10677 Solved: 4313[Submit ...
- SPOJ 375 Query on a tree(树链剖分)(QTREE)
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- js基础拖拽效果
function drag(ele) { const config = { mark: 0, x: 0, y: 0, left: ele.offsetLeft, top: ele.offsetTop, ...
- Ionic开发-常用插件安装
cordova plugin add cordova-plugin-nativestoragecordova plugin add cordova-plugin-devicecordova plu ...
- pytorch 安装错误,报 GLIBCXX_3.4.20 错误
pytorch 从源码安装 链接:http://blog.csdn.net/u012442157/article/details/78134888 发现错误: 解决方案: http://blog.cs ...
- go实现主线程等待子线程都运行完再退出
方式一 package main import ( "fmt" ) func main() { ch := make(chan struct{}) count := 2 // co ...
- Java中的break循环——通过示例学习Java编程(13)
作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=24 break语句通常用于以下两种情况: (A)使 ...
- I/O————流
流的关系图 缓冲流分为字节和字符缓冲流(图中是经常用的搭配,PrintWrite与BufferedWrite都继承java.io.Write) 字节缓冲流为: BufferedInputStream— ...
- File类。
File类: java.io.File 类.是文件和文件夹目录名的抽象表示形式. 可以用File对文件和文件夹进行 创建,删除,获取等操作. File类的一些静态成员变量: static String ...
- HDU 3530Subsequence(单调队列)
题意 题目链接 给出$n$个数,找出最长的区间,使得区间中最大数$-$最小数 $>= m$ 且$<= k$ Sol 考虑维护两个单调队列. 一个维护$1 - i$的最大值,一个维护$1 - ...
- mui实现图片更换(暂未上传)
页面中有默认的图片,触发type为file的input时,更换图片,这个是mui移动端的项目,算了,不多说,开码 首先,先在html页面中设置样式,样式我就不给了,贴个布局 <div class ...
- awk对列求和
awk 'BEGIN{total=0}{total+=$1}END{print total}' 文件名