题目链接: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. Silverlight Color的颜色值

    1.MainPage.xaml <UserControl xmlns:SysManage="clr-namespace:Application" x:Class=" ...

  2. 一,U盘安装 CentOS 6.5 minimal

    U盘安装盘: CentOS-6.5的版本有四个,分别是: 1.CentOS-6.5-i386-netinstall.iso 通过网络安装的,需要联网 2.CentOS-6.5-i386-minimal ...

  3. 1)C++对象大小计算

          C++对象的大小不同的编译器的实现是不一样的,以下仅讨论.net2003,其他编译的可能出现的结果以下也做了分析和猜测.在反推不同编译器实现的C++对象的大小时.对齐是一个很重要也容易被遗 ...

  4. 007-python基础-pyc是什么

    3.1 解释型语言和编译型语言 计算机是不能够识别高级语言的,所以当我们运行一个高级语言程序的时候,就需要一个"翻译机"来从事把高级语言转变成计算机能读懂的机器语言的过程.这个过程 ...

  5. openstack做镜像

    virt-install --virt-type kvm --name windows2008 --ram 1024 --cdrom /opt/windows-2008-x86_64.iso --di ...

  6. JForum二次开发(一)

    1.环境 myeclipse2014,jdk7,tomcat8,mysql5.6 2.下载源码地址 http://jforum.net/download.jsp 3.导入源码 新建web工程JForu ...

  7. JQuery中ajax跨域问题

    var url = "http://apis.juhe.cn/idcard/index?key=e0a6277420506b2816b82f7d7821976c&cardno=&qu ...

  8. PHP(一)

    最近一段时间一直忙于新版本的开发工作,所以虽然自己脑中有一些想法,但是苦于没有足够的时间去写下来.好了,昨天终于将大体的功能开发完成,时间上面也不会那么的紧张了.下来我想要好好的梳理一下,自己最近一段 ...

  9. keepalived安装配置(nginx)

    环境:centos 6.4 64bit 应用:nginx 目的:keepalived可以让两台服务器处于主备关系,如果主的挂了,备的取得VIP(或者互为主备等关系,文字游戏不纠结), 以实现服务器的高 ...

  10. MYSQL主键存在则更新,不存在则插入的解决方案(ON DUPLICATE KEY UPDATE)

    经常我们使用的最简单的数据库操作就是数据的更新,删除和插入,对于批量删除和插入的方法相信大家都很清楚,那么批量更新估计有的人就不知道了,并且还有批量插入,在插入时若有主键冲突则更新的操作,这在EAV模 ...