spoj375Query on a tree
来源:互联网 发布:北龙中网域名认证 编辑:程序博客网 时间:2024/06/14 04:20
题意:
Problem code: QTREE
You are given a tree (an acyclic undirected connected graph) withN 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 integersa b c denotes an edge between a, b of costc (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
解法:传说之中仰望依旧的树链剖分,根据节点的规模将一棵树进行剖分,然后分为若干条链。这些链可以看做是一条一条的线段,对应到一条长线段之中。然后用线段树维护,看起来很复杂其实并不难,尤其是这一道题的只用到最简单单点更新的线段树,难度不大。详情见代码//// main.cpp// query on a tree//// Created by 蘇與軒 on 15/2/13.// Copyright (c) 2015年 蘇與軒. All rights reserved.//#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <cmath>#include <cstdlib>#include <string>#include <map>#include <set>#include <algorithm>#include <functional>#define rep(i,a,b) for (int i=a;i<((b)+1);i++)#define Rep(i,a,b) for (int i=a;i>=b;i--)#define foreach(e,x) for (__typeof(x.begin()) e=x.begin();e!=x.end();e++)#define mid ((l+r)>>1)#define lson (k<<1)#define rson (k<<1|1)#define MEM(a,x) memset(a,x,sizeof a)using namespace std;const int N=20050;typedef pair<int, int> pii;typedef long long ll;int pnt[N],head[N],nxt[N],cnt,T,n,re[N][3],root;int tree[N],son[N],top[N],size[N],fa[N],dep[N],w[N],z;void addedge(int u,int v,int c) { pnt[cnt]=v;nxt[cnt]=head[u];head[u]=cnt++;}void dfs(int u) { size[u]=1;son[u]=0; for (int i=head[u];i!=-1;i=nxt[i]) { int v=pnt[i]; if (v!=fa[u]){ fa[v]=u; dep[v]=dep[u]+1; dfs(v); if (size[v]>size[son[u]]) son[u]=v; size[u]+=size[v]; } }}void build_tree(int u,int tp) { top[u]=tp;w[u]=++z; if (son[u]!=0) build_tree(son[u], tp); for (int i=head[u];i!=-1;i=nxt[i]) { int v=pnt[i]; if (v!=son[u]&&v!=fa[u]) build_tree(v, v); }}void update(int k,int l,int r,int pos,int x) { if (pos>r||pos<l) return ; if (l==r) { tree[k]=x; return ; } update(lson,l,mid,pos,x); update(rson, mid+1, r, pos, x); tree[k]=max(tree[lson],tree[rson]);}int query(int k,int l,int r,int ll,int rr) { if (ll>r||rr<l) return 0; if (ll<=l&&rr>=r) return tree[k]; return max(query(lson,l,mid,ll,rr),query(rson, mid+1, r, ll, rr));}int find(int x,int y) { int f1=top[x],f2=top[y],res=0; while (f1!=f2) { if (dep[f1]<dep[f2]){ swap(f1,f2); swap(x,y); } res=max(res,query(1,1,z,w[f1],w[x])); x=fa[f1];f1=top[x]; } if (x==y) return res; if (dep[x]>dep[y]) swap(x, y); return max(res,query(1, 1, z, w[son[x]], w[y]));}int main(int argc, const char * argv[]) { scanf("%d",&T); while (T--) { scanf("%d",&n); MEM(head,-1);MEM(re,0);MEM(nxt,-1);MEM(size,0);MEM(tree,0); root=(n+1)/2; fa[root]=cnt=z=dep[root]=0; rep(i,1,n-1) { int a,b,c; scanf("%d%d%d",&a,&b,&c); addedge(a,b,c); addedge(b,a,c); re[i][0]=a;re[i][1]=b;re[i][2]=c; } dfs(root); build_tree(root,root); rep(i,1,n-1) { if (dep[re[i][0]]>dep[re[i][1]]) swap(re[i][0],re[i][1]); update(1,1,z,w[re[i][1]],re[i][2]); } char s[20]; while (~scanf("%s",s)) { if (s[0]=='D') break; int a,b; scanf("%d%d",&a,&b); if (s[0]=='Q') printf("%d\n",find(a,b)); else update(1, 1, z, w[re[a][1]], b); } } return 0;}
0 0
- spoj375Query on a tree
- Query on A Tree
- 3804 Query on a tree
- poj1909 Marbles on a tree
- SPOJ Query on a tree
- Query on a tree II
- [SPOJ375]Query on a tree
- SPOJ375 Query on a tree
- hdu_3804_Query on a tree(树链剖分)
- SPOJ QTREEQuery on a tree
- SPOJ375 Query on a tree
- [hackerrank]Counting on a tree
- spoj375 Query on a tree
- SPOJ Count on a tree
- SPOJ Query on a tree
- bzoj2588 Count on a tree
- Count on a tree SPOJ
- SPOJ Query on a tree
- HTTP 方法:GET 对比 POST
- activeMQ之点对点(PTP),监听器模式
- C++学习 STL之五:set/multiset用法详解
- HDU 1711 Number Sequence (数字KMP)
- Android自定义View,你必须知道的几点
- spoj375Query on a tree
- Linux GCC常用命令
- C++学习 STL之六:map/multimap用法详解
- C++ 重载赋值操作符
- the info.plist in the package must contain the CFBundleShortVersinString key
- Sicily 1166 Computer Transformat
- 新架构下的html代码
- 两个字符串中最大的公共字串
- java反射机制