人生第一道树链剖分的题目,其实树链剖分并不是特别难。

思想就是把树剖成一些轻链和重链,轻链比较少可以直接修改,重链比较长,用线段树去维护。

貌似大家都是从这篇博客上学的。

 #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的更多相关文章

  1. SPOJ 375 树链剖分

    SPOJ太慢了,SPOJ太慢了, 题意:给定n(n<=10000)个节点的树,每条边有边权,有两种操作:1.修改某条变的边权:2.查询u,v之间路径上的最大边权. 分析:树链剖分入门题,看这里: ...

  2. SPOJ 375 (树链剖分 - 边权剖分 - 修改单边权)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/I 给你一棵有边权的树,有两个操作:一个操作是输出l到 ...

  3. SPOJ 375 (树链剖分+线段树)

    题意:一棵包含N 个结点的树,每条边都有一个权值,要求模拟两种操作:(1)改变某条边的权值,(2)询问U,V 之间的路径中权值最大的边. 思路:最近比赛总是看到有树链剖分的题目,就看了论文,做了这题, ...

  4. spoj 375 树链剖分 模板

    QTREE - Query on a tree #tree You are given a tree (an acyclic undirected connected graph) with N no ...

  5. spoj 375 树链剖分模板

    /* 只是一道树链刨分的入门题,作为模板用. */ #include<stdio.h> #include<string.h> #include<iostream> ...

  6. 数据结构(并查集||树链剖分):HEOI 2016 tree

    [注意事项] 为了体现增强版,题目限制和数据范围有所增强: 时间限制:1.5s 内存限制:128MB 对于15% 的数据,1<=N,Q<=1000. 对于35% 的数据,1<=N,Q ...

  7. 树链剖分【CF343D】Water Tree

    Description Mad scientist Mike has constructed a rooted tree, which consists of nnvertices. Each ver ...

  8. bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 10677  Solved: 4313[Submit ...

  9. 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, ...

随机推荐

  1. css3响应式表格

    <h1>极客学院相关课程</h1> <table class="responsive"> <thead> <tr> &l ...

  2. text-transform 字母的大小写

    text-transform: none   默认 capitalize    每个单词以大写字母开头 uppercase    仅有大写字母 lowercase     无大写字母,仅有小写字母 i ...

  3. Promise 对象与Generator 函数

    异步编程的方法,大概有下面四种: 回调函数 事件监听 发布/订阅 Promise 对象 传统的编程语言,早有异步编程的解决方案(其实是多任务的解决方案).其中有一种叫做"协程"(c ...

  4. 31全志r58平台Android4.4.2下打开USB摄像头

    31全志r58平台Android4.4.2下打开USB摄像头 2018/10/26 16:00 版本:V1.0 开发板:SC5806 1.系统编译:(略) 2.需要修改的文件: W:\r58_andr ...

  5. 【虚拟机-虚拟网络】使用 PsPing & PaPing 进行 TCP 端口连通性测试

    PsPing & PaPing 介绍 通常,我们测试数据包能否通过 IP 协议到达特定主机时,都习惯使用 ping 命令.工作时 ping 向目标主机发送一个 IMCP Echo 请求的数据包 ...

  6. .NET easyUI tree树状结构

    简单的制作后台制作写一个json(string类型)格式 public partial class goodstype_type : System.Web.UI.Page { public strin ...

  7. PostgreSQL缓存

    目录[-] pg_buffercache pgfincore pg_prewarm dstat Linux ftools 使用pg_prewarm预加载关系/索引: pgfincore 输出: 怎样刷 ...

  8. “IIS7.5无法写入配置文件web.config”的解决方案

    原因 用了非NTSF(如:FAT32或exFAT分区格式,导致无法设置安全选项,从而导致无法自动写入文件). 解决方案 先在默认网站配置好,然后把web.config复制到目标网站的根目录,这样就等于 ...

  9. jquery的uploadify插件实现的批量上传V3.2.1版

    你需要如下配置(包括引入文件)HTML: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&quo ...

  10. 监控服务端口状态python脚本

    #!/usr/bin/python import socket,os,time data={ 8080:"tomcat9", 18080:"tomcat_hjgdmj&q ...