思路

注意本题只能用C,不能用C++

其他的都和上一题一样

代码

#include <stdio.h>
#include <string.h>
#define MAXN 10010
int dfs_clock,dep[MAXN*2],heason[MAXN*2],id[MAXN*2],sz[MAXN*2],top[MAXN*2],fa[MAXN*2],w_p[MAXN*2],v[MAXN*2],fir[MAXN*2],nxt[MAXN*2],w[MAXN*2],val[MAXN*2],maxx[MAXN*5],cnt;
struct Edge{
int u,v,w;
}E[MAXN*2];
int max(int a,int b){
return (a>b)?a:b;
}
void addedge(int ui,int vi,int wi){
++cnt;
v[cnt]=vi;
w[cnt]=wi;
nxt[cnt]=fir[ui];
fir[ui]=cnt;
}
void dfs1(int u,int f,int wx){
fa[u]=f;
sz[u]=1;
w_p[u]=wx;
for(int i=fir[u];i;i=nxt[i]){
if(v[i]==f)
continue;
dep[v[i]]=dep[u]+1;
dfs1(v[i],u,w[i]);
sz[u]+=sz[v[i]];
if(heason[u]==0||sz[v[i]]>sz[heason[u]])
heason[u]=v[i];
}
}
void dfs2(int u,int f,int topf){
id[u]=++dfs_clock;
val[id[u]]=w_p[u];
top[u]=topf;
if(!heason[u])
return;
dfs2(heason[u],u,topf);
for(int i=fir[u];i;i=nxt[i]){
if(v[i]==f||v[i]==heason[u])
continue;
dfs2(v[i],u,v[i]);
}
}
void pushup(int o){
maxx[o]=max(maxx[o<<1],maxx[o<<1|1]);
}
void build(int l,int r,int o){
if(l==r){
maxx[o]=val[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,o<<1);
build(mid+1,r,o<<1|1);
pushup(o);
}
void update1(int l,int r,int pos,int o,int c){
if(l==r){
maxx[o]=c;
return;
}
int mid=(l+r)>>1;
if(pos<=mid)
update1(l,mid,pos,o<<1,c);
else
update1(mid+1,r,pos,o<<1|1,c);
pushup(o);
}
void update(int o,int c){
update1(1,dfs_clock,id[o],1,c);
}
int query1(int L,int R,int l,int r,int o){
if(L<=l&&r<=R)
return maxx[o];
int mid=(l+r)>>1,ans=0;
if(L<=mid)
ans=max(ans,query1(L,R,l,mid,o<<1));
if(R>mid)
ans=max(ans,query1(L,R,mid+1,r,o<<1|1));
return ans;
}
int query(int x,int y){
int ans=0;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]){
int t=x;
x=y;
y=t;
}
ans=max(ans,query1(id[top[x]],id[x],1,dfs_clock,1));
x=fa[top[x]];
}
if(dep[x]>dep[y]){
int t=x;
x=y;
y=t;
}
ans=max(ans,query1(id[x],id[y],1,dfs_clock,1));
return ans;
}
int lca(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]){
int t=x;
x=y;
y=t;
}
x=fa[top[x]];
}
return (dep[x]<dep[y])?x:y;
}
void init(void){
dfs_clock=0;
memset(dep,0,sizeof(dep));
memset(heason,0,sizeof(heason));
memset(id,0,sizeof(id));
memset(sz,0,sizeof(sz));
memset(top,0,sizeof(top));
memset(fa,0,sizeof(fa));
memset(w_p,0,sizeof(w_p));
memset(v,0,sizeof(v));
memset(fir,0,sizeof(fir));
memset(nxt,0,sizeof(nxt));
memset(w,0,sizeof(w));
memset(val,0,sizeof(val));
memset(maxx,0,sizeof(maxx));
cnt=0;
memset(E,0,sizeof(E));
}
int n,T;
int main(){
scanf("%d",&T);
while(T--){
init();
scanf("%d",&n);
for(int i=1;i<n;i++){
scanf("%d %d %d",&E[i].u,&E[i].v,&E[i].w);
addedge(E[i].u,E[i].v,E[i].w);
addedge(E[i].v,E[i].u,E[i].w);
}
dfs1(1,0,0);
dfs2(1,0,1);
build(1,dfs_clock,1);
char opt[10];
while(1){
scanf("%s",&opt);
if(opt[0]=='D')
break;
if(opt[0]=='Q'){
int a,b;
scanf("%d %d",&a,&b);
int LCA=lca(a,b);
// printf("lca=%d\n",LCA);
int t=w_p[LCA];
update(LCA,0);
int ans=query(a,b);
update(LCA,t);
printf("%d\n",ans);
}
else{
int a,b;
scanf("%d %d",&a,&b);
int t=(dep[E[a].u]>dep[E[a].v])?E[a].u:E[a].v;
w_p[t]=b;
update(t,b);
}
}
}
return 0;
}

SPOJ 375 QTREE - Query on a tree的更多相关文章

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

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

  2. SPOJ VJudge QTREE - Query on a tree

    Query on a tree Time Limit: 851MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submi ...

  3. 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 ...

  4. QTREE - Query on a tree

    QTREE - Query on a tree 题目链接:http://www.spoj.com/problems/QTREE/ 参考博客:http://blog.sina.com.cn/s/blog ...

  5. SP375 QTREE - Query on a tree (树剖)

    题目 SP375 QTREE - Query on a tree 解析 也就是个蓝题,因为比较长 树剖裸题(基本上),单点修改,链上查询. 顺便来说一下链上操作时如何将边上的操作转化为点上的操作: 可 ...

  6. SPOJ 375 树链剖分 QTREE - Query on a tree

    人生第一道树链剖分的题目,其实树链剖分并不是特别难. 思想就是把树剖成一些轻链和重链,轻链比较少可以直接修改,重链比较长,用线段树去维护. 貌似大家都是从这篇博客上学的. #include <c ...

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

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

  8. SPOJ QTREE Query on a tree --树链剖分

    题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. ...

  9. SPOJ QTREE Query on a tree VI

    You are given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes are number ...

随机推荐

  1. spring batch (二) 元数据表

    内容来自<Spring Batch 批处理框架>,作者:刘相. 一.spring batch 框架进行元数据管理共有六张表,三张SEQUENCE用来分配主键的,九张表分别是: BATCH_ ...

  2. 出现error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

    查看自己python的版本,然后下载自己版本Python的devel,比如python3.6.8就是 sudo apt-get install python3.6-dev

  3. iOS与H5交互(WKWbebView)

    前言: 在iOS开发中,或多或少的会嵌入一些H5页面,有时候需要原生代码和H5页面进行交互.iOS8开始苹果推出性能更强大的WKWebView,所以一下方法是关于WKWebView与JS的交互. 创建 ...

  4. jsp四大作用域

  5. Leetcode Articles: Insert into a Cyclic Sorted List

    Given a node from a cyclic linked list which has been sorted, write a function to insert a value int ...

  6. Angular5入门与搭建项目

    步骤 1. 设置开发环境 在开始工作之前,你必须设置好开发环境. 如果你的电脑里没有 Node.js®和 npm,请安装它们. 请先在终端/控制台窗口中运行命令 node -v 和 npm -v, 来 ...

  7. Hadoop集群故障诊断

    集群故障诊断通行方法:1.cloudera manager 监控和管理软件本身出问题了(没有任何数据),集群还是好的,业务还在正常跑:2.监控软件是好的,从监控里发现了很多问题,如CPU飙高.内存飙高 ...

  8. ELK实时日志分析平台环境部署

    为什么要用到ELK一般我们需要进行日志分析场景是:直接在日志文件中 grep.awk 就可以获得自己想要的信息.但在规模较大的场景中,此方法效率低下,面临问题包括日志量太大如何归档.文本搜索太慢怎么办 ...

  9. vs2017 git凭证问题

    安装完tfs2017(自带git)后,连接git代码服务器,报authorication错误.这是vs2017的bug.尝试了许多种方案后,最简单的干脆手动添加如下3个凭证.一定要核对好地址中的.  ...

  10. codeforces-4

    这题使用到了类似于双数据 Maximal Continuous #include<iostream> #include<algorithm> #include<stdio ...