题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值。

解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个。

询问的时候,在从u找到v的过程中顺便查询到此为止的最大值即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 10007 int siz[N]; //子树大小
int son[N]; //重儿子
int dep[N]; //深度
int pos[N]; //在线段树中的位置
int Top[N]; //所在重链的祖先
int fa[N]; //父节点
int ans[N]; //答案
int head[*N],tot,POS,n,m;
struct Edge
{
int v,next;
}G[*N];
int tree[*N];
struct node
{
int u,v,w;
}edge[N]; void init()
{
POS = tot = ;
memset(head,-,sizeof(head));
memset(son,-,sizeof(son));
memset(tree,,sizeof(tree));
} void addedge(int u,int v)
{
G[tot].v = v, G[tot].next = head[u], head[u] = tot++;
G[tot].v = u, G[tot].next = head[v], head[v] = tot++;
} void pushup(int rt)
{
tree[rt] = max(tree[*rt],tree[*rt+]);
} void update(int l,int r,int pos,int val,int rt)
{
if(l == r)
{
tree[rt] = val;
return;
}
int mid = (l+r)/;
if(pos <= mid)
update(l,mid,pos,val,*rt);
else
update(mid+,r,pos,val,*rt+);
pushup(rt);
} int query(int l,int r,int aa,int bb,int rt)
{
if(aa <= l && bb >= r)
return tree[rt];
int mid = (l+r)/;
if(bb <= mid) return query(l,mid,aa,bb,*rt);
else if(aa > mid) return query(mid+,r,aa,bb,*rt+);
return max(query(l,mid,aa,bb,*rt),query(mid+,r,aa,bb,*rt+));
} void dfs(int u,int f)
{
dep[u] = dep[f]+;
siz[u] = ;
for(int i=head[u];i!=-;i=G[i].next)
{
int v = G[i].v;
if(v == f) continue;
fa[v] = u;
dfs(v,u);
if(son[u] == - || siz[v] > siz[son[u]]) son[u] = v;
siz[u] += siz[v];
}
} void dfs2(int u,int father)
{
pos[u] = ++POS;
Top[u] = father;
if(son[u] != -) dfs2(son[u],father);
for(int i=head[u];i!=-;i=G[i].next)
{
int v = G[i].v;
if(v != fa[u] && v != son[u])
dfs2(v,v);
}
} int ask(int u,int v)
{
int fx = Top[u], fy = Top[v], maxi = ;
while(fx != fy)
{
if(dep[fx] < dep[fy])
{
swap(u,v);
swap(fx,fy);
}
maxi = max(maxi,query(,POS,pos[fx],pos[u],));
u = fa[fx];
fx = Top[u];
}
if(u == v) return maxi;
if(dep[u] > dep[v]) swap(u,v);
return max(maxi,query(,POS,pos[son[u]],pos[v],));
} int main()
{
int u,v,w,x,y,i,t;
char ss[];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init();
for(i=;i<n;i++)
{
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
addedge(edge[i].u,edge[i].v);
}
dep[] = ;
dfs(,);
dfs2(,);
for(i=;i<n;i++)
{
if(dep[edge[i].u] > dep[edge[i].v])
swap(edge[i].u,edge[i].v);
update(,POS,pos[edge[i].v],edge[i].w,);
}
while(scanf("%s",ss)!=EOF && ss[] != 'D')
{
if(ss[] == 'Q')
{
scanf("%d%d",&u,&v);
printf("%d\n",ask(u,v));
}
else
{
scanf("%d%d",&x,&y);
update(,POS,pos[edge[x].v],y,);
}
}
if(t >= )
puts("");
}
return ;
}

SPOJ QTREE Query on a tree --树链剖分的更多相关文章

  1. SPOJ QTREE Query on a tree 树链剖分+线段树

    题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an a ...

  2. spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)

    传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...

  3. SPOJ QTREE Query on a tree ——树链剖分 线段树

    [题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...

  4. spoj 375 QTREE - Query on a tree 树链剖分

    题目链接 给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值. 树链剖分模板. #include <iostream> #includ ...

  5. spoj 375 Query on a tree (树链剖分)

    Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges ...

  6. SPOJ 375 Query on a tree 树链剖分模板

    第一次写树剖~ #include<iostream> #include<cstring> #include<cstdio> #define L(u) u<&l ...

  7. SPOJ Query on a tree 树链剖分 水题

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  8. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

  9. Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...

随机推荐

  1. java猜数字游戏

    import java.util.Scanner; //导入包 class GuessNum { public static void main(String[] args) { int num = ...

  2. python 任意新闻正文提取

    在github上搜到一个正文提取程序,测试了一下基本可以对现在大多数大型新闻网站进行提取 后续我会分析一下这个程序的源码 使用非常简单 如下 # -*- coding: utf-8 -*- impor ...

  3. 细说Mysql四种安装方法及自动化部署

    一.简介 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 我们也可以将数据存储在文件中,但是 ...

  4. LinQ实战学习笔记(三) 序列,查询操作符,查询表达式,表达式树

    序列 延迟查询执行 查询操作符 查询表达式 表达式树 (一) 序列 先上一段代码, 这段代码使用扩展方法实现下面的要求: 取进程列表,进行过滤(取大于10M的进程) 列表进行排序(按内存占用) 只保留 ...

  5. 桥牌笔记L4D17:小心阻塞

    南打3NT. 西的首攻会有3墩黑桃.3墩方块.2付梅花,共8墩到手.看来方块如果3-2分布的话,非常容易就能超一完成. 所以要想着4-1分布的安全打法. 第一墩庄家拿了黑桃J后,明手的黑桃A会阻塞,庄 ...

  6. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q111-Q114)

    Question 111You create a custom page layout that contains the following code segment. (Line numbers ...

  7. Android 正则表达式

    1.相关知识链接 正则表达式语法   https://msdn.microsoft.com/zh-cn/library/ae5bf541(v=vs.80).aspx 正则表达式入门教程   http: ...

  8. 安卓第四天笔记-Sqlite

    安卓第四天笔记-Sqlite 1.数据库的创建运行与更新 1.1.创建一个类继承SqliteOpenHelper 1.2.创建构造方法 /** * 数据库创建类 * @author 刘楠 * * 20 ...

  9. docker入门(1) Centos 7 下docker的安装

    centos 7安装docker 什么是 Docker Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go ...

  10. (转)为什么大公司青睐Java

    转自 http://www.zhihu.com/question/25908953/answer/32119971 因为这是一个商业问题,不是技术问题. 我在面试时探讨过这个问题,对方创业期,问我如果 ...