题意:给一棵树,每次更新某条边或者查询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. Follow me to learn what is repository pattern

    Introduction Creating a generic repository pattern in an mvc application with entity framework is th ...

  2. 【GOF23设计模式】组合模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_组合模式.树状结构.杀毒软件架构.JUnite底层架构.常见开发场景 package com.test.composite ...

  3. Visual Studio添加dll程序集引用操作步骤

    Visual Studio 中添加引用的操作: 在“解决方案资源管理器”中,先右击项目图标,在弹出菜单选择“添加引用...” 然后在弹出的窗口中选择所要添加的选项,点击确定就可以了. 原文:http: ...

  4. Vue数据绑定

    gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson04 一 双括号用来数据绑定 (1)写法一: {{message}}, ...

  5. mvc项目架构分享系列之架构搭建之Infrastructure

    项目架构搭建之Infrastructure的搭建 Contents 系列一[架构概览] 0.项目简介 1.项目解决方案分层方案 2.所用到的技术 3.项目引用关系 系列二[架构搭建初步] 4.项目架构 ...

  6. 同源策略和JSONP(概念性)

    同源策略 浏览器有一个很重要的概念——同源策略(Same-Origin Policy). 所谓同源是指,域名,协议,端口相同.不同源的客户端脚本(javascript.ActionScript)在没明 ...

  7. GP的使用心得

    在ArcEngine时,GP无疑是GIS开发者的神器.自ArcEngine9.2开始新增一个程序集ESRI.ArcGIS.Geoprocessor,它能调用包含扩展模块在内的所有Geoprocessi ...

  8. SharePoint 2013: Search Architecture in SPC202

    http://social.technet.microsoft.com/wiki/contents/articles/15989.sharepoint-2013-search-architecture ...

  9. 安装sql server managerment studio报错"The instance id is required but it is missing"

    问题描述: 今天在安装sql server managerment studio的时候提示报错"The instance id is required but it is missing&q ...

  10. [Android]Android Debug key 的制作

    Android Debug key 的制作 背景 在Android App 开发过程中,我们经常会使用一些第三方的服务,但是很多的第三方服务都会要求我们提供包名,签名安装包,这时候,我们在日常调试时, ...