spoj 375 树链剖分模板
/*
只是一道树链刨分的入门题,作为模板用。
*/
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<map>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define N 11000
#define inf 0x3fffffff
int head[N];
int son[N];//记录与当前点相连的数目最多的子节点的下标
int fa[N];//记录上一个父节点
int siz[N];//记录当前节点的子节点的数目
int top[N];//当前链的最顶层
int f[N];//重新标记
int fp[N];//记录重新标记前的点
int deep[N];//深度
int w[N];// 记录当前点与其父节点的关系
int nu,yong;
int Max;
struct nodee
{
int u,v,w,next;
} bian[N*4],ff[N];
void init()
{
yong=nu=0;
memset(head,-1,sizeof(head));
memset(son,-1,sizeof(son));
}
void addedge(int u,int v,int w)
{
bian[yong].u=u;
bian[yong].v=v;
bian[yong].next=head[u];
head[u]=yong++;
}
void dfs(int u,int father,int d)
{
deep[u]=d;//记录深度
siz[u]=1;//初始化
fa[u]=father;//记录父节点
int i;
for(i=head[u]; i!=-1; i=bian[i].next)
{
int v=bian[i].v;
if(v!=father)
{
dfs(v,u,d+1);//
siz[u]+=siz[v];//回溯时累加数目
if(son[u]==-1||siz[son[u]]<siz[v])//son[u]记录当前点相连的点的子节点数目最多的点,即重链
son[u]=v;//u-v
}
}
}
void getnu(int u,int cnt)
{
top[u]=cnt;//记录当前链的顶端
f[u]=nu++;//重新标记
fp[f[u]]=u;//记录标记前的编号
if(son[u]==-1)return ;//如果他没有儿子节点
getnu(son[u],cnt);//重链
int i;
for(i=head[u]; i!=-1; i=bian[i].next)
{
int v=bian[i].v;
if(v!=fa[u]&&v!=son[u])//排除重链
getnu(v,v);//轻链
}
return;
}
//******以上求重链和轻链以及各部分的初始化,下面是线段树部分和查询询问**********//
struct node
{
int l,r,maxx;
} tree[N*4];
int Ma(int v,int vv)
{
return v>vv?v:vv;
}
void pushup(int t) //回溯时更新最大值和最小值
{
tree[t].maxx=Ma(tree[t*2].maxx,tree[t*2+1].maxx);
}
void build(int t,int l,int r)//建树
{
tree[t].l=l;
tree[t].r=r;
if(tree[t].l==tree[t].r)
{
tree[t].maxx=w[tree[t].l];//记录边权值
return ;
}
int mid=(l+r)>>1;
build(t*2,l,mid);
build(t*2+1,mid+1,r);
pushup(t);
}
void qury(int t,int l,int r)//询问区间的最大值
{
if(tree[t].l==l&&tree[t].r==r)//如果查到
{
Max=Ma(Max,tree[t].maxx);//
return ;
}
int mid=(tree[t].l+tree[t].r)>>1;
if(r<=mid)
qury(t*2,l,r);
else if(l>mid)
qury(t*2+1,l,r);
else
{
qury(t*2,l,mid);
qury(t*2+1,mid+1,r);
}
pushup(t);
}
int findmax(int u,int v)//查找最大值
{
int f1=top[u];//得到顶端编号值
int f2=top[v];
int ans=-inf;//初始化最小值
while(f1!=f2)//结束条件,再通一个重链上
{
if(deep[f1]<deep[f2])//从最深层开始网上
{
swap(f1,f2);//交换
swap(u,v);
}
Max=-inf;
qury(1,f[f1],f[u]);//询问重新编号后的f[u]和其顶端节点之间的最大值,从而使其从f[u]跳到顶端
ans=Ma(ans,Max);//ans储存最大值
u=fa[f1];//从f1向上跳一步,不管当前链是轻链还是重链
f1=top[u];//得到跳一步后的顶端节点,继续比较
}//
if(v==u)return ans;//如果在同一点就直接返回
if(deep[u]>deep[v])swap(u,v);//得到u,v之间的最小值
Max=-inf;
qury(1,f[son[u]],f[v]);//求出u的子节点的f[v]---f[u的最大数目子节点];,因为此时他们在一个重链上
ans=Ma(ans,Max);//求出最大值
return ans;
}
void update(int t,int x,int y)//更新
{
if(tree[t].l==x&&tree[t].r==x)//
{
tree[t].maxx=y;
return ;
}
int mid=(tree[t].l+tree[t].r)/2;
if(x<=mid)
update(t*2,x,y);
else
update(t*2+1,x,y);
pushup(t);
}
int main()
{
int T,i,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
init();
for(i=1; i<n; i++)
{
scanf("%d%d%d",&ff[i].u,&ff[i].v,&ff[i].w);
addedge(ff[i].u,ff[i].v,ff[i].w);
addedge(ff[i].v,ff[i].u,ff[i].w);
}
dfs(1,1,0);//深搜的到 每个节点的深度,父节点和其子节点的数目(包括本身),还有最大数目的子节点的编号
getnu(1,1);//得到重链或者轻链的顶端和重新编号并记录重新编号前的值。如果是轻链的话
for(i=1; i<n; i++)
{
if(deep[ff[i].u]<deep[ff[i].v])
swap(ff[i].u,ff[i].v);//得到深度最大的节点
w[f[ff[i].u]]=ff[i].w;//记录重新编号后的当前点与上一个点的权值
}
build(1,1,nu-1);//建树
char s[222];
int x,y;
while(scanf("%s",s),strcmp(s,"DONE"))
{
scanf("%d%d",&x,&y);
if(s[0]=='Q')
printf("%d\n",findmax(x,y));//找区间最大值
else
update(1,f[ff[x].u],y);//更换区间中某个值,ff[x].u是深度较大的数,所以不会出现越界情况,即f[ff[x].u]不为0
}
}
return 0;
}
spoj 375 树链剖分模板的更多相关文章
- spoj 375 树链剖分 模板
QTREE - Query on a tree #tree You are given a tree (an acyclic undirected connected graph) with N no ...
- 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
人生第一道树链剖分的题目,其实树链剖分并不是特别难. 思想就是把树剖成一些轻链和重链,轻链比较少可以直接修改,重链比较长,用线段树去维护. 貌似大家都是从这篇博客上学的. #include <c ...
- BZOJ 2243 染色 | 树链剖分模板题进阶版
BZOJ 2243 染色 | 树链剖分模板题进阶版 这道题呢~就是个带区间修改的树链剖分~ 如何区间修改?跟树链剖分的区间询问一个道理,再加上线段树的区间修改就好了. 这道题要注意的是,无论是线段树上 ...
- 算法复习——树链剖分模板(bzoj1036)
题目: 题目背景 ZJOI2008 DAY1 T4 题目描述 一棵树上有 n 个节点,编号分别为 1 到 n ,每个节点都有一个权值 w .我们将以下面的形式来要求你对这棵树完成一些操作:I.CHAN ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- QTREE 树链剖分---模板 spoj QTREE
<树链剖分及其应用> 一文讲得非常清楚,我一早上就把他学会了并且A了这题的入门题. spoj QTREE 题目: 给出一棵树,有两种操作: 1.修改一条边的边权. 2.询问节点a到b的最大 ...
随机推荐
- bzoj 1016: [JSOI2008]最小生成树计数【dfs+克鲁斯卡尔】
有一个性质就是组成最小生成树总边权值的若干边权总是相等的 这意味着按边权排序后在权值相同的一段区间内的边能被选入最小生成树的条数是固定的 所以先随便求一个最小生成树,把每段的入选边数记录下来 然后对于 ...
- 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman
题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...
- [转]深入ASP.NET MVC之九:Ajax支持
本文转自:http://www.cnblogs.com/yinzixin/archive/2012/12/22/2824384.html 目前前端页面和服务端进行Ajax交互大多采用的都是jQuery ...
- Java 8 (9) Optional取代null
NullPointerException,大家应该都见过.这是Tony Hoare在设计ALGOL W语言时提出的null引用的想法,他的设计初衷是想通过编译器的自动检测机制,确保所有使用引用的地方都 ...
- arp学习笔记(linux高性能服务编程)
先看看arp的定义吧 现在linux运行这条命令 tcpdump -i eth0:1 -ent '(dst 192.168.5.190 and src 192.168.5.109)or( dst 19 ...
- leetcode377 Combination Sum IV
思路: dp. 实现: class Solution { public: int combinationSum4(vector<int>& nums, int target) { ...
- 数据库–Cobar分布式数据库集群MySQL中间件
运行环境: 主机1:Ubuntu14.04 Desktop + MySQL5.5 + JDK 1.7(HP Z400) 内网IP地址:192.168.137.8 NODE1:Ubuntu 13.04 ...
- Codeforces_758_D_(区间dp)
D. Ability To Convert time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Call stack Structure
The stack frame at the top of the stack is for the currently executing routine. Th ...
- 网络编程基础_4.1TCP_服务端
TCP_服务端 #include <stdio.h> // 1. 包含必要的头文件和库, 必须位于 windows之前 #include <WinSock2.h> #pragm ...