Query on a tree

Time Limit: 5000ms
Memory Limit: 262144KB
 
This problem will be judged on SPOJ. Original ID: QTREE
64-bit integer IO format: %lld      Java class name: Main
Font Size: + -
Type:  
None

Graph Theory

2-SAT

Articulation/Bridge/Biconnected Component

Cycles/Topological Sorting/Strongly Connected Component

Shortest Path

Bellman Ford

Dijkstra/Floyd Warshall

Euler Trail/Circuit

Heavy-Light Decomposition

Minimum Spanning Tree

Stable Marriage Problem

Trees

Directed Minimum Spanning Tree

Flow/Matching

Graph Matching

Bipartite Matching

Hopcroft–Karp Bipartite Matching

Weighted Bipartite Matching/Hungarian Algorithm

Flow

Max Flow/Min Cut

Min Cost Max Flow

DFS-like

Backtracking with Pruning/Branch and Bound

Basic Recursion

IDA* Search

Parsing/Grammar

Breadth First Search/Depth First Search

Advanced Search Techniques

Binary Search/Bisection

Ternary Search

Geometry

Basic Geometry

Computational Geometry

Convex Hull

Pick's Theorem

Game Theory

Green Hackenbush/Colon Principle/Fusion Principle

Nim

Sprague-Grundy Number

Matrix

Gaussian Elimination

Matrix Exponentiation

Data Structures

Basic Data Structures

Binary Indexed Tree

Binary Search Tree

Hashing

Orthogonal Range Search

Range Minimum Query/Lowest Common Ancestor

Segment Tree/Interval Tree

Trie Tree

Sorting

Disjoint Set

String

Aho Corasick

Knuth-Morris-Pratt

Suffix Array/Suffix Tree

Math

Basic Math

Big Integer Arithmetic

Number Theory

Chinese Remainder Theorem

Extended Euclid

Inclusion/Exclusion

Modular Arithmetic

Combinatorics

Group Theory/Burnside's lemma

Counting

Probability/Expected Value

Others

Tricky

Hardest

Unusual

Brute Force

Implementation

Constructive Algorithms

Two Pointer

Bitmask

Beginner

Discrete Logarithm/Shank's Baby-step Giant-step Algorithm

Greedy

Divide and Conquer

Dynamic Programming

Tag it!

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 ab 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
 #include<iostream>
#include<stdio.h>
#include<cstdlib>
#include<cstring>
using namespace std;
const int maxn = 1e4+; struct node
{
int l,r,Max;
}f[maxn*]; struct Edge
{
int to,next;
}edge[maxn*]; int e[maxn][];
int p[maxn];
int top[maxn];
int siz[maxn];
int son[maxn];
int deep[maxn];
int father[maxn];
int head[maxn];
int num[maxn];
int cont,pos; void init()
{
cont = ;
pos = ;
memset(head,-,sizeof(head));
memset(son,-,sizeof(son));
}
void add(int n1,int n2)
{
edge[cont].to=n2;// 指向谁
edge[cont].next=head[n1];
head[n1]=cont;
cont++;
} void dfs1(int u,int pre,int d)/**fa deep,num,son**/
{
deep[u]=d;
father[u]=pre;
num[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v = edge[i].to;
if(v!=pre)
{
dfs1(v,u,d+);
num[u]=num[u]+num[v];
if(son[u]==- || num[v]>num[son[u]])
son[u]=v;
}
}
} void getops(int u,int sp)
{
top[u]=sp;
if(son[u]!=-)
{
p[u]=++pos;
getops(son[u],sp);
}
else
{
p[u]=++pos;
return;
}
for(int i=head[u];i!=-;i=edge[i].next)
{
int v = edge[i].to;
if(v!=son[u] && v!=father[u])
getops(v,v);
}
}
void build(int l,int r,int n)
{
f[n].l=l;
f[n].r=r;
f[n].Max=;
if(l==r)return;
int mid=(l+r)/;
build(l,mid,n*);
build(mid+,r,n*+);
}
int query(int l,int r,int n)
{
int mid=(f[n].l+f[n].r)/;
int ans1,ans2;
if(f[n].l==l && f[n].r==r) return f[n].Max;
if(mid>=r) return query(l,r,n*);
else if(mid<l) return query(l,r,n*+);
else
{
ans1=query(l,mid,n*);
ans2=query(mid+,r,n*+);
if(ans1<ans2) ans1=ans2;
}
return ans1;
}
void update(int x,int num1,int n)
{
int mid=(f[n].l+f[n].r)/;
if(f[n].l == x && f[n].r == x)
{
f[n].Max=num1;
return;
}
if(mid>=x) update(x,num1,n*);
else update(x,num1,n*+);
f[n].Max = f[n*].Max>f[n*+].Max? f[n*].Max:f[n*+].Max;
}
int find(int u,int v)
{
int f1 = top[u],f2 = top[v];
int MAX=;
while(f1!=f2)
{
if(deep[f1]<deep[f2])
{
swap(f1,f2);
swap(u,v);
}
MAX=max(MAX,query(p[f1],p[u],));
u=father[f1];
f1=top[u];
}
if(u==v)return MAX;
if(deep[u]>deep[v])swap(u,v);
return max(MAX,query(p[son[u]],p[v],));
}
int main()
{
int T,n,l,r,x,num1;
char a[];
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
init();
for(int i=;i<n;i++)
{
scanf("%d%d%d",&e[i][],&e[i][],&e[i][]);
add(e[i][],e[i][]);
add(e[i][],e[i][]);
}
dfs1(,,);
getops(,);
build(,pos,);
for(int i=;i<n;i++)
{
if(deep[e[i][]]>deep[e[i][]]) swap(e[i][],e[i][]);
update(p[e[i][]],e[i][],);
}
while(scanf("%s",a)>)
{
if(a[]=='D')break;
if(a[]=='Q')
{
scanf("%d%d",&l,&r);
printf("%d\n",find(l,r));
}
else if(a[]=='C')
{
scanf("%d%d",&x,&num1);
update(p[e[x][]],num1,);
}
}
}
return ;
}

SPOJ 375. Query on a tree (树链剖分)的更多相关文章

  1. spoj 375 Query on a tree (树链剖分)

    Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges ...

  2. SPOJ 375 Query on a tree 树链剖分模板

    第一次写树剖~ #include<iostream> #include<cstring> #include<cstdio> #define L(u) u<&l ...

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

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

  5. SPOJ QTREE Query on a tree ——树链剖分 线段树

    [题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...

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

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

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

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

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

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

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

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

  10. Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...

随机推荐

  1. [转]Oracle数据库ASH和AWR的简单介绍

    在Oracle数据库中,有时我们可能会遇到这样的术语:ASH和AWR,那么它们是怎样产生的呢?它们的作用又是什么呢?本文我们就来介绍这一部分内容.       1.10g之前 用户的连接将产生会话,当 ...

  2. 非静态的字段、方法或属性“System.Web.UI.Page.ClientScript...”要求对象引用 (封装注册脚本)

    在写项目时想对asp.net的注册前台脚本事件进行封装,就添加了一个BasePage.cs页面,但一直报错‘非静态的字段.方法或属性“System.Web.UI.Page.ClientScript.. ...

  3. 您的服务器没有安装这个PHP扩展:OpenSSL(其他平台API同步需要)

    今天在安装一个博客系统的时候提示这个错,在网上找了半天,自己慢慢弄出来的,具体如下: 1.找到你的php.ini 文件,将“;extension=php_openssl.dll”前面分号去掉. 2.复 ...

  4. zju(3)内核编译与运行

    1.实验目的 学习和掌握Linux配置和编译的基本步骤. 二.实验内容 1. 对Linux内核及用户程序进行配置: 2. 编译生成内核映像文件: 3. 把编译的映像文件烧写到FLASH中,查看运行结果 ...

  5. WooCommerce代码收集

    修改首页和分类页面每页产品数量 每页显示多少产品默认跟随设置 » 阅读设置 » 博客页面至多显示的值,若要产品索引页和博文索引页使用不同的设置,可以使用下面的代码为产品索引页单独设置每页产品数. ad ...

  6. 20145334赵文豪 《Java程序设计》第8周学习总结

    20145334赵文豪 <Java程序设计>第8周学习总结 教材学习内容总结 转眼间Java学习已经到了第八周,第十五章,需要我们学会使用日志API.了解国际化基础.认识JDK8增强功能等 ...

  7. 转载:有关qsort的使用方法和注意事项

    七种qsort排序方法 <本文中排序都是采用的从小到大排序> 一.对int类型数组排序 int num[100]; Sample: int cmp ( const void *a , co ...

  8. JavaScript入门篇 第三天(认识DOM)

    认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面代码 ...

  9. Unity学习疑问记录之正交与透视

    Unity中相机的投影是2种方式,正交和透视 这是透视方式 正交方式: //计算屏幕宽度 float height = 2.0f * Camera.main.orthographicSize;//正交 ...

  10. bootstrap全局CSS样式学习

    参考http://v3.bootcss.com/css/,根据自己的记忆进行的复述,加深记忆. 首先介绍bootstrap全局CSS样式 只通过使用bootstrap.css,即可获得统一的样式设置. ...