SPOJ375[QTREE
来源:互联网 发布:apache hadoop怎么读 编辑:程序博客网 时间:2024/06/18 10:27
Descripion:
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
solution:树链剖分
#include <cstdio>#include <iostream>#include <cstring>using namespace std;const int N = 1e4 + 7;struct Edge{ int nxt, to;}e[N<<1];int head[N], tot=0;void addeage(int u, int v){ e[++tot].nxt=head[u], e[tot].to=v; head[u]=tot;}int eg[N][4], pos[N], sz[N], top[N], fat[N], dep[N], son[N];int id=0, n;void dfs1(int u, int fa){ son[u]=0; sz[u]=1; dep[u]=dep[fa]+1; for ( int i=head[u]; i; i=e[i].nxt ){ int v=e[i].to; if( v==fa ) continue; fat[v]=u; dfs1(v, u); sz[u]+=sz[v]; if( sz[v]>sz[son[u]] ) son[u]=v; }} void dfs2(int u, int fa, int fq){ pos[u]=++id; top[u]=fq; if( son[u] ){ dfs2(son[u], u, fq); } for ( int i=head[u]; i; i=e[i].nxt ){ int v=e[i].to; if( v==fa || v==son[u] ) continue; dfs2(v, u, v); }}#define ls nd<<1#define rs nd<<1|1struct Node{ int maxv;}tr[N<<2];void pushup(int nd){ tr[nd].maxv=max(tr[ls].maxv, tr[rs].maxv);}void modify(int nd, int l, int r, int p, int val){ if( l==r ){ tr[nd].maxv=val; return ; } int mid=(l+r)>>1; if( mid>=p ) modify(ls, l, mid, p, val); else modify(rs, mid+1, r, p, val); pushup(nd);}int query(int nd, int l, int r, int L, int R){ if( L<=l && r<=R ){ return tr[nd].maxv; } int mid=(l+r)>>1; int mx=0; if( mid>=L ) mx=max(mx, query(ls, l, mid, L, R)); if( mid<R ) mx=max(mx, query(rs, mid+1, r, L, R)); return mx; } void swap(int &a, int &b){ a=a^b; b=a^b; a=a^b;}int query2(int u, int v){ int f1=top[u], f2=top[v]; int ret=0; while( f1!=f2 ){ if( dep[f1]<dep[f2] ) swap(f1,f2), swap(u, v);// printf("%d %d\n", pos[f1], pos[u] ); ret=max(ret, query(1,1,n,pos[f1],pos[u]) ); u=fat[f1]; f1=top[u]; } if( u==v ) return ret; if( dep[u]>dep[v] ) swap(u, v); ret=max(ret, query(1,1,n,pos[son[u]],pos[v]) ); return ret;}void init(){ memset(head,0,sizeof(head)); tot=0; id=0;}int main(){ int T; scanf("%d", &T); while( T-- ){ init(); scanf("%d", &n ); for ( int i=1; i<n; i++ ){ int x, y, z; scanf("%d%d%d", &x, &y, &z ); eg[i][1]=x, eg[i][2]=y, eg[i][3]=z; addeage(x,y), addeage(y,x); } dfs1(1,1); dfs2(1,1,1);// for ( int i=1; i<=n; i++ ) printf("%d ", pos[i] );// printf("\n"); for ( int i=1; i<n; i++ ){ if( dep[eg[i][1]]<dep[eg[i][2]] ) swap(eg[i][1], eg[i][2]); modify(1,1,n,pos[eg[i][1]],eg[i][3]);// printf("%d %d\n", pos[eg[i][1]], eg[i][3] ); } for (;;){ char ch[10]; int x, y; scanf("%s", ch); if( ch[0]=='D' ) { break; } scanf("%d%d", &x, &y ); if( ch[0]=='Q' ){ printf("%d\n", query2(x,y) ); }else if( ch[0]=='C' ){ modify(1,1,n,pos[eg[x][1]],y); } } }}
- SPOJ375.QTREE
- [SPOJ375] QTREE
- SPOJ375[QTREE
- Spoj375 Qtree
- SPOJ375 QTREE 树链剖分入门
- 【COGS1672】【SPOJ375】QTREE
- SPOJ375 QTREE - Query on a tree
- spoj375
- SPOJ375(树链剖分)
- 树链剖分 spoj375
- spoj375 树链剖分
- spoj375 重学树链剖分
- spoj375 树链剖分
- 树链剖分-spoj375
- spoj375(树链剖分)
- SPOJ QTREE
- qtree(树链刨分)
- SPOJ QTREE
- pci 随记
- java中构造方法及其作用
- 【维护区间最长连续子序列 && 线段树 && 区间归并】HDU
- Linux vi/vim编辑器常用命令与用法总结
- linux kali 下安装 微 信
- SPOJ375[QTREE
- 【USACO TRAINING】子集的和
- 京东轮播图
- [Leetcode] 369. Plus One Linked List 解题报告
- Android自定义Transition动画
- 《深入理解java虚拟机》学习-第八章-虚拟机字节码执行引擎-运行时栈帧结构
- iOS开发-获取设备IP
- 解决spring请求中文乱码问题
- 高性能web服务器nginx