题目链接:http://www.spoj.com/problems/QTREE/en/

QTREE - Query on a tree

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

We will ask you to perfrom some instructions of the following form:

  • CHANGE i ti : change the cost of the i-th edge to ti
    or
  • QUERY a b : ask for the maximum edge cost on the path from node a to node b

Input

The first line of input contains an integer t, the number of test cases (t <= 20). t test cases follow.

For each test case:

  • In the first line there is an integer N (N <= 10000),
  • In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 1000000),
  • The next lines contain instructions "CHANGE i ti" or "QUERY a b",
  • The end of each test case is signified by the string "DONE".

There is one blank line between successive tests.

Output

For each "QUERY" operation, write one integer representing its result.

Example

Input:
1 3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE Output:
1
3

题意:给你一颗数,求u到v的路径中最大的边权大小;

思路:线段树单点更新区间查询;

    树链剖分板子;

   最后把边权往下落,去掉lca那个点的权值;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=2e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=; ///数组大小
struct edge{
int v,next;
}edge[N<<];
int head[N<<],edg,id,n;
/// 树链剖分 int fa[N],dep[N],son[N],siz[N]; // fa父亲,dep深度,son重儿子,siz以该点为子树的节点个数
int a[N],ran[N],top[N],tid[N]; // tid表示边的标号,top通过重边可以到达最上面的点,ran表示标记tid
int u[N],v[N],w[N];
void init()
{
memset(son,-,sizeof(son));
memset(head,-,sizeof(head));
edg=;
id=;
}
void add(int u,int v)
{
edg++;
edge[edg].v=v;
edge[edg].next=head[u];
head[u]=edg;
}
void dfs1(int u,int fath,int deep)
{
fa[u]=fath;
siz[u]=;
dep[u]=deep;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(v==fath)continue;
dfs1(v,u,deep+);
siz[u]+=siz[v];
if(son[u]==-||siz[v]>siz[son[u]])
son[u]=v;
}
}
void dfs2(int u,int tp)
{
tid[u]=++id;
top[u]=tp;
ran[tid[u]]=u;
if(son[u]==-)return;
dfs2(son[u],tp);
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(v==fa[u])continue;
if(v!=son[u])
dfs2(v,v);
}
} /// 线段树
int sum[N<<];
void pushup(int pos)
{
sum[pos]=max(sum[pos<<],sum[pos<<|]);
}
void build(int l,int r,int pos)
{
if(l==r)
{
sum[pos]=a[ran[l]];
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
void update(int p,int c,int l,int r,int pos)
{
if(l==r)
{
sum[pos]=c;
return;
}
int mid=(l+r)>>;
if(p<=mid)update(p,c,l,mid,pos<<);
if(p>mid) update(p,c,mid+,r,pos<<|);
pushup(pos);
}
int query(int L,int R,int l,int r,int pos)
{
if(L<=l&&r<=R)return sum[pos];
int mid=(l+r)>>;
int ans=;
if(L<=mid)ans=max(ans,query(L,R,l,mid,pos<<));
if(R>mid) ans=max(ans,query(L,R,mid+,r,pos<<|));
return ans;
}
int up(int l,int r)
{
int ans=;
while(top[l]!=top[r])
{
if(dep[top[l]]<dep[top[r]])swap(l,r);
ans=max(ans,query(tid[top[l]],tid[l],,n,));
l=fa[top[l]];
}
if(dep[l]==dep[r])return ans;
if(dep[l]<dep[r])swap(l,r);
//cout<<tid[r]<<" "<<tid[l]<<" "<<endl;
ans=max(ans,query(tid[son[r]],tid[l],,n,));
return ans;
}
char s[];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
init();
for(int i=;i<n;i++)
{
scanf("%d%d%d",&u[i],&v[i],&w[i]);
add(u[i],v[i]);
add(v[i],u[i]);
}
dfs1(,-,);
dfs2(,);
for(int i=;i<n;i++)
{
if(fa[u[i]]==v[i])a[u[i]]=w[i];
else a[v[i]]=w[i];
}
build(,n,);
while()
{
scanf("%s",s);
if(s[]=='D')break;
int a,b;
scanf("%d%d",&a,&b);
if(s[]=='C')
{
if(fa[u[a]]==v[a])update(tid[u[a]],b,,n,);
else update(tid[v[a]],b,,n,);
}
else
{
printf("%d\n",up(a,b));
}
}
}
return ;
}

SPOJ QTREE Query on a tree 树链剖分+线段树的更多相关文章

  1. Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)

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

  2. 【POJ3237】Tree(树链剖分+线段树)

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

  3. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  4. POJ3237 Tree 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...

  5. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

  6. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  7. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  8. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  9. BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )

    BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...

  10. Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

随机推荐

  1. 计蒜客--移除数组中的重复元素 (set)

    给定一个升序排列的数组,去掉重复的数,并输出新的数组的长度. 例如:数组 A = \{1, 1, 2\}A={1,1,2},你的程序应该输出 22 即新数组的长度,新数组为 \{1, 2\}{1,2} ...

  2. Mysql初级第二天(wangyun)

    SQL 1.LIKE 操作符 SELECT 列名称 FROM 表名称 WHERE 列 LIKE 值('N%'/'%N%'/'%N','N_') SELECT 列名称 FROM 表名称 WHERE 列 ...

  3. 再论sklearn分类器

    https://www.cnblogs.com/hhh5460/p/5132203.html 这几天在看 sklearn 的文档,发现他的分类器有很多,这里做一些简略的记录. 大致可以将这些分类器分成 ...

  4. 今日总结(linux和plsql)

    #case ...when语句(根据字段不同值显示不同结果) ##1)case ...when语句的使用方法一: 语法格式: case column_name when value1 then res ...

  5. PYQT5 + PYCHARM

    PYQT5 C:\Users\xxx\AppData\Local\Programs\Python\Python37\Lib\site-packages\pyqt5_tools\designer.exe ...

  6. c++ STL中的set和multiset

    1.结构 set和multiset会根据特定的排序原则将元素排序.两者不同之处在于,multisets允许元素重复,而set不允许重复. set中的元素可以是任意类型的,但是由于需要排序,所以元素必须 ...

  7. gnats配置文件

    尽管NATS可以无配置的运行,但也可以使用配置文件配置NATS服务器 1. 配置项包括 客户端监听器端口 Client listening port HTTP监听器端口 HTTP monitoring ...

  8. oracle 12.2 linux/solaris正式发布

    oracle 12.2 linux/solaris正式发布,可以从http://www.oracle.com/technetwork/database/enterprise-edition/downl ...

  9. charles最新破解jar文件,及浏览器证书安装

    一 在线破解文件下载 Charles 在线破解工具,https://www.zzzmode.com/mytools/charles/ 描述 此工具用于生成破解后的charles.jar文件,  破解原 ...

  10. Javascropt-KeyCode

    当点回车时触发搜索事件 $(document).keydown(function (e) {                if (e.keyCode == 13) {                 ...