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. 演示一个OLS进行数据访问控制的示例

    1.确认数据库版本 2.安装OLS组件 3.创建策略 4.创建分级和标签 5.创建测试用户并授权 6.更新标签 7.测试演示

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

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

  3. div自定义的滚动条 (水平导航条)

    <!DOCTYPE html> <html> <head> <title></title> <style> div{ /* wi ...

  4. iphone6 帶回家”活動!

    十一小長假即將來臨,周向榮還準備窩在家裏坐等“鋒菲戀”的後續結果嗎?雖然宅男無罪,但是請不要繼續在論壇裏高呼“李亞鵬娶了張柏芝”等口號,放下你“不吐槽會死星人”的特質,走出家門去領略一下祖國的大好山河 ...

  5. 数据库---T-SQL语句提纲

    T-SQL语句: 创建表:create table Name(Code varchar(50)) 主键:primary key自增长列:auto_increment外键关系:references非空: ...

  6. Spinner的深入学习

    简介: spinner是一个列表选择框,会在用户选择后,展示一个列表供用户进行选择.Spinner是ViewGroup的间接子类,它和其他的Android控件一样,数据需要使用Adapter进行封装. ...

  7. A Great Alchemist

    Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB ProblemCarol is a great alchemist. In ...

  8. 【iCore3 双核心板】DEMO 1.0 测试程序发布

    iCore3 Demo V1.0 程序说明 一.概要 本资料包包含5个文件夹: 1.“arm”里是 icore3上 arm的程序包,开发环境为 KEIL 5.17: 2.“fpga”里是 icore3 ...

  9. Opensuse enable sound and mic card

    Install application pavucontrol Run pavucontrol You will see the configuration about sound card and ...

  10. NSMutableAttributedString可变属性字符串的用法

    适用于:当你想对一个字符串中的某几个字符更改颜色,字体... NSString *string = @"今日营养配餐提供热量1800千卡,需要饮食之外额外补充钙10mg,铁20mg,锌9.5 ...