题目链接:http://poj.org/problem?id=3237

You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbered 1 through N − 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions can be one of the following forms:

CHANGE i v Change the weight of the ith edge to v
NEGATE a b Negate the weight of every edge on the path from a to b
QUERY a b Find the maximum weight of edges on the path from a to b

题意描述:一棵树有n个节点和n-1条边,每条边有一个权值。现在给出三种操作:

CHANGE I V:把第i条边的值改为v

NEGATE A B:把A到B的路径上的所有边的值取反(正为负,负改为正)

QUERY A B:询问A到B的路径上的边权值的最大值。

算法分析:树链剖分解决。把边权值移到节点上面,由于操作上有对值取反,所有我们不止要运用线段树统计区间最大值maxnum,还要统计区间最小值minnum,这样在取反操作后,maxnum=-maxnum,minnum=-minnum,再把两个值交换:swap(maxnum,minnum)即可。

说明:阅读了一些大牛的代码,感觉线段树部分还是结构体比数组方便一些,树链剖分刚开始学,代码和解题思想很多是借鉴大牛们的,只是把代码风格改成自己的了,相信只有不断学习和解题才会对树链剖分有一定理解的。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#define inf 0x7fffffff
using namespace std;
const int maxn=+; struct Edge
{
int to,next;
}edge[maxn*];
int head[maxn],edgenum;
int top[maxn];//top[v]表示v所在的重链的顶端节点
int fa[maxn]; //父亲节点
int dep[maxn];//深度
int siz[maxn];//siz[v]表示以v为根的子树的节点数
int tid[maxn];//tid[v]表示v与其父亲节点的连边在线段树中的位置
int tid2[maxn];//和tid数组相反
int son[maxn];//重儿子
int pos;
void init()
{
edgenum=;
memset(head,-,sizeof(head));
pos=;
memset(son,-,sizeof(son));
}
void addedge(int u,int v)
{
edge[edgenum].to=v ;edge[edgenum].next=head[u];
head[u]=edgenum++; edge[edgenum].to=u ;edge[edgenum].next=head[v];
head[v]=edgenum++;
} void dfs1(int u,int pre,int d) //第一遍dfs求出fa,dep,siz,son
{
dep[u]=d;
fa[u]=pre;
siz[u]=;
for (int i=head[u] ;i != - ;i=edge[i].next)
{
int v=edge[i].to;
if (v != pre)
{
dfs1(v,u,d+);
siz[u] += siz[v];
if (son[u] == - || siz[v]>siz[son[u]])
son[u]=v;
}
}
}
void dfs2(int u,int tp) //第二遍dfs求出top和tid
{
top[u]=tp;
tid[u]= ++pos;
tid2[pos]=u;
if (son[u] == -) return;
dfs2(son[u],tp);
for (int i=head[u] ;i != - ;i=edge[i].next)
{
int v=edge[i].to;
if (v != son[u] && v != fa[u])
dfs2(v,v);
}
} //线段树
struct node
{
int l,r;
int Max;
int Min;
int ne;
}segTree[maxn*]; void build(int l,int r,int rt)
{
segTree[rt].l=l;
segTree[rt].r=r;
segTree[rt].Max=;
segTree[rt].Min=;
segTree[rt].ne=;
if (l==r) return ;
int mid=(l+r)/;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
}
void PushUP(int rt)
{
segTree[rt].Max = max(segTree[rt<<].Max,segTree[rt<<|].Max);
segTree[rt].Min = min(segTree[rt<<].Min,segTree[rt<<|].Min);
}
void PushDown(int rt)
{
if (segTree[rt].l == segTree[rt].r) return ;
if (segTree[rt].ne)
{
segTree[rt<<].Max = -segTree[rt<<].Max;
segTree[rt<<].Min = -segTree[rt<<].Min;
swap(segTree[rt<<].Min,segTree[rt<<].Max);
segTree[rt<<|].Max = -segTree[rt<<|].Max;
segTree[rt<<|].Min = -segTree[rt<<|].Min;
swap(segTree[rt<<|].Max,segTree[rt<<|].Min);
segTree[rt<<].ne ^= ;
segTree[rt<<|].ne ^= ;
segTree[rt].ne = ;
}
} void update(int k,int val,int rt) // 更新线段树的第k个值为val
{
if(segTree[rt].l == k && segTree[rt].r == k)
{
segTree[rt].Max = val;
segTree[rt].Min = val;
segTree[rt].ne = ;
return;
}
PushDown(rt);
int mid = (segTree[rt].l + segTree[rt].r)/;
if(k <= mid)update(k,val,rt<<);
else update(k,val,(rt<<)|);
PushUP(rt);
}
void ne_update(int l,int r,int rt) // 更新线段树的区间[l,r]取反
{
if (segTree[rt].l == l && segTree[rt].r == r)
{
segTree[rt].Max = -segTree[rt].Max;
segTree[rt].Min = -segTree[rt].Min;
swap(segTree[rt].Max,segTree[rt].Min);
segTree[rt].ne ^= ;
return;
}
PushDown(rt);
int mid = (segTree[rt].l + segTree[rt].r)/;
if (r <= mid) ne_update(l,r,rt<<);
else if (l > mid) ne_update(l,r,(rt<<)|);
else
{
ne_update(l,mid,rt<<);
ne_update(mid+,r,(rt<<)|);
}
PushUP(rt);
}
int query(int l,int r,int rt) //查询线段树中[l,r] 的最大值
{
if (segTree[rt].l == l && segTree[rt].r == r)
return segTree[rt].Max;
PushDown(rt);
int mid = (segTree[rt].l+segTree[rt].r)>>;
if (r <= mid) return query(l,r,rt<<);
else if (l > mid) return query(l,r,(rt<<)|);
else return max(query(l,mid,rt<<),query(mid+,r,(rt<<)|));
PushUP(rt);
}
int findmax(int u,int v)//查询u->v边的最大值
{
int f1 = top[u], f2 = top[v];
int tmp = -;
while(f1 != f2)
{
if(dep[f1] < dep[f2])
{
swap(f1,f2);
swap(u,v);
}
tmp = max(tmp,query(tid[f1],tid[u],));
u = fa[f1]; f1 = top[u];
}
if(u == v)return tmp;
if(dep[u] > dep[v]) swap(u,v);
return max(tmp,query(tid[son[u]],tid[v],));
} void Negate(int u,int v)
{
int f1=top[u],f2=top[v];
while (f1 != f2)
{
if (dep[f1]<dep[f2])
{
swap(f1,f2);
swap(u,v);
}
ne_update(tid[f1],tid[u],);
u=fa[f1] ;f1=top[u];
}
if (u==v) return;
if (dep[u]>dep[v]) swap(u,v);
return ne_update(tid[son[u] ],tid[v],);
} int e[maxn][];
int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d",&n);
for(int i = ;i < n-;i++)
{
scanf("%d%d%d",&e[i][],&e[i][],&e[i][]);
addedge(e[i][],e[i][]);
}
dfs1(,,);
dfs2(,);
build(,n,);
for (int i = ;i < n-; i++)
{
if (dep[e[i][]]>dep[e[i][]])
swap(e[i][],e[i][]);
update(tid[e[i][]],e[i][],);
}
char op[];
int u,v;
while (scanf("%s",op) == )
{
if (op[] == 'D') break;
scanf("%d%d",&u,&v);
if (op[]=='Q')
printf("%d\n",findmax(u,v));//查询u->v路径上边权的最大值
else if (op[]=='C')
update(tid[e[u-][]],v,);//改变第u条边的值为v
else Negate(u,v);
}
}
return ;
}

poj 3237 Tree 树链剖分的更多相关文章

  1. POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)

    题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...

  2. POJ 3237.Tree -树链剖分(边权)(边值更新、路径边权最值、区间标记)贴个板子备忘

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12247   Accepted: 3151 Descriptio ...

  3. poj 3237 Tree 树链剖分+线段树

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  4. poj 3237 Tree(树链拆分)

    题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...

  5. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  6. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

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

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

  8. 【BZOJ-4353】Play with tree 树链剖分

    4353: Play with tree Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 31  Solved: 19[Submit][Status][ ...

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

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

随机推荐

  1. 转载字典地址:http://blog.csdn.net/aladdinty/article/details/3591789

    相关文章: http://www.360doc.com/content/13/1003/23/14070959_318861279.shtml http://www.360doc.com/conten ...

  2. Session为null 问题

    问题描述: var svode=HttpContext.Current.Session["VCode"].ToString(); //调试时候发现 svode ==null // ...

  3. Cron和Spring定时任务

    1.Java Spring spring定时任务cronExpression的值(配置定时时间)格式说明: 一个cronExpression表达式有至少6个(也可能是7个)由空格分隔的时间元素.从左至 ...

  4. HTTP首部及各状态码

    通用首部:客户端和服务器都可以使用的通用首部,比如Status Code: 请求首部:请求首部是请求报文特有的,它们为服务器提供了一些额外信息,例如Accept: */* 用来告知服务器客户端会接受与 ...

  5. 透过c的编程原则,来规范自己现在的一些编程习惯

    1.合理的使用注释 注释为:/*…………*/ 注释有以下几种情况: 1) 版本.版权声明. 2) 函数接口说明. 3) 重要的代码或者段落显示. 注释注意: 1) 注释是对代码的解释,不是对文档.注释 ...

  6. java android 访问DELPHI 的DATASNAP

    最新版的DELPHI开发DATASNAP非常简单便捷,DataSnap的REST风格和对JSON的支持,使之成为服务器端开发的神器. 一.DATASNAP服务器中的方法: TServerMethods ...

  7. jQuery学习笔记(5)--表单域获得焦点和失去焦点样式变化

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  8. p ython笔记第一天

    一.Linux基础 - 计算机以及日后我们开发的程序防止的服务器的简单操作 二.Python开发 http://www.cnblogs.com/wupeiqi/articles/5433893.htm ...

  9. Sublime Text 2编译python时出错

    [Error 2] The system cannot find the file specified [Finished]   解决方法: 1.环境变量path添加: C:\Python32\Too ...

  10. C实现辗转相除法求两个数的最大公约数

    什么是辗转相除法? 辗转相除法(又名欧几里德算法),它主要用于求两个正整数的最大公约数.是已知的最古老的算法. 用辗转相除法求132和72的最大公约数的步骤: 132 / 72 = 1 ... 60 ...