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. javascript平时小例子⑤(投票效果的练习)

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  2. php Use of undefined constant的问题解决方式

    在每个文件头上加 error_reporting(0); 或者 搜索php.ini: error_reporting = E_ALL 改为: error_reporting = E_ALL & ...

  3. 【iCore3 双核心板】例程三十:U_DISK_IAP_FPGA实验——更新升级FPGA

    实验指导书及代码包下载: http://pan.baidu.com/s/1jH1TiKY iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  4. Python之什么是函数

    我们知道圆的面积计算公式为: S = πr² 当我们知道半径r的值时,就可以根据公式计算出面积.假设我们需要计算3个不同大小的圆的面积: r1 = 12.34 r2 = 9.08 r3 = 73.1 ...

  5. JVM监控和Java应用程序调试

    JConsole.VisualVM监控JVM(JMX) JAVA_OPTS后加:-Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.managemen ...

  6. 论MySQL的监控和调优

    懂PHP的人一般都懂MySQL这一点不假,大多数书籍里也是这样,书中前面讲PHP后面到数据库这块就会讲到MySQL的一些知识,前几年MySQL一直是PHP书籍的一部分,后来开始从国外翻译了一些专门讲述 ...

  7. JS之原型对象

    1.__proto__ 每个对象都有一个__proto__属性,指向该对象的原型对象 <script> var person = function(name,city){ this.nam ...

  8. 白话学习MVC(七)Action的执行一

    一.概述 在此系列开篇的时候介绍了MVC的生命周期 , 对于请求的处理,都是将相应的类的方法注册到HttpApplication事件中,通过事件的依次执行从而完成对请求的处理.对于MVC来说,请求是先 ...

  9. mongoose index

    1. 当应用程序启动时,Mongoose会自动为模式中的每个定义的索引调用ensureIndex. 虽然很好用于开发,但建议在生产中禁用此行为,因为索引创建可能会导致显着的性能影响. 通过将模式的au ...

  10. python RabbitMQ队列/redis

    RabbitMQ队列 rabbitMQ是消息队列:想想之前的我们学过队列queue:threading queue(线程queue,多个线程之间进行数据交互).进程queue(父进程与子进程进行交互或 ...