动态树(Link Cut Tree) :SPOJ 375 Query on a tree
2016-02-26 21:12
501 查看
QTREE - Query on a tree
#number-theoryYou 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 tiorQUERY a b : ask for the maximum edge cost on the path from node a to node bInput
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 这题可以用树链剖分做,我这里用LCT做的,代码量更少。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn=10010; int Max[maxn],fa[maxn],ch[maxn][2],key[maxn]; bool rt[maxn]; void Push_up(int p) { Max[p]=max(key[p],max(Max[ch[p][0]],Max[ch[p][1]])); } void Rotate(int x) { int y=fa[x],g=fa[y],c=ch[y][1]==x; ch[y][c]=ch[x][c^1];ch[x][c^1]=y; fa[ch[y][c]]=y;fa[y]=x;fa[x]=g; if(rt[y]) rt[y]=false,rt[x]=true; else ch[g][ch[g][1]==y]=x; Push_up(y); } void Splay(int x) { for(int y=fa[x];!rt[x];Rotate(x),y=fa[x]) if(!rt[y]) Rotate((ch[fa[y]][1]==y)==(ch[y][1]==x)?y:x); Push_up(x); } void Access(int x) { int y=0; while(x){ Splay(x); rt[ch[x][1]]=true; rt[ch[x][1]=y]=false; Push_up(x); x=fa[y=x]; } } void Query(int x,int y) { Access(y),y=0; while(true) { Splay(x); if(!fa[x]){ printf("%d\n",max(Max[y],Max[ch[x][1]])); return; } rt[ch[x][1]]=true; rt[ch[x][1]=y]=false; Push_up(x); x=fa[y=x]; } } void Change(int x,int d) { Access(x); Splay(x); key[x]=d; Push_up(x); } int fir[maxn],nxt[maxn<<1],to[maxn<<1],cnt; int e[maxn][3]; void addedge(int a,int b) { nxt[++cnt]=fir[a];to[cnt]=b;fir[a]=cnt; } void DFS(int node) { for(int i=fir[node];i;i=nxt[i]) { if(fa[to[i]])continue; fa[to[i]]=node; DFS(to[i]); } } void Init() { cnt=0;Max[0]=-1000000000; for(int i=1;i<=10000;i++){ Max[i]=fir[i]=fa[i]=0; rt[i]=1; } return; } int main() { int T,n,a,b; scanf("%d",&T); while(T--) { Init(); scanf("%d",&n); for(int i=1;i<n;i++) scanf("%d%d%d",&e[i][0],&e[i][1],&e[i][2]); for(int i=1;i<n;i++) addedge(e[i][0],e[i][1]),addedge(e[i][1],e[i][0]); fa[1]=-1; DFS(1); fa[1]=0; for(int i=1;i<n;i++) { if(fa[e[i][0]]==e[i][1]) swap(e[i][0],e[i][1]); Change(e[i][1],e[i][2]); } char op[10]; while(true) { scanf("%s",op); if(!strcmp(op,"DONE"))break; else if(!strcmp(op,"QUERY")){ scanf("%d%d",&a,&b); Query(a,b); } else { scanf("%d%d",&a,&b); Change(e[a][1],b); } } } return 0; }
相关文章推荐
- <LeetCode OJ> 96. Unique Binary Search Trees
- Google推出iOS功能性UI测试框架EarlGrey
- 使用selenuim自动操作浏览器
- 1003_Fibonacci Sequence
- [Locked] Closest Binary Search Tree Value & Closest Binary Search Tree Value II
- 简单方式处理特效UI夹层
- iPhone开发之实现UITableView多选删除功能详解
- Java并发-类库新组件 - PriorityBlockingQueue 理解
- NGUI层级深度理解总结
- 设置UITableViewCell分割线顶格
- iOS UI-popoverController
- Cocoa Touch事件处理流程--响应者链
- 9. UIActionSheet
- 树状数组 + 位运算 LA 4013 A Sequence of Numbers
- 【poj3581】Sequence 后缀数组
- Java并发-类库新组件 - DelayQueue 理解
- Espresso ui单元测试框架初探
- Android进程等级分类、UI线程操作方式
- 安卓学习笔记(一)——线程的用法及怎样在子线程中更新UI
- [Unit Testing] Directive testing, require parent controller