博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spoj 375 Query on a tree (树链剖分)
阅读量:5747 次
发布时间:2019-06-18

本文共 3452 字,大约阅读时间需要 11 分钟。

Query on a tree

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:131 2 12 3 2QUERY 1 2CHANGE 1 3QUERY 1 2DONEOutput:13 第一次接触树链剖分,貌似是用来处理对树的边权的多次询问,然后对边权进行编号,转化为节点之间的询问。具体关于树链剖分的解析见 http://blog.csdn.net/acdreamers/article/details/10591443
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define met(a,b) memset(a,b,sizeof a)#define pb push_back#define lson(x) ((x<<1))#define rson(x) ((x<<1)+1)using namespace std;typedef long long ll;const int N=1e5+50;const int M=N*N+10;int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N]; //top 最近的重链父节点int num;vector
v[N];struct tree { int x,y,val; void read() { scanf("%d%d%d",&x,&y,&val); }};tree e[N];void dfs1(int u, int f, int d) { dep[u] = d; siz[u] = 1; son[u] = 0; fa[u] = f; for (int i = 0; i < v[u].size(); i++) { int ff = v[u][i]; if (ff == f) continue; dfs1(ff, u, d + 1); siz[u] += siz[ff]; if (siz[son[u]] < siz[ff]) son[u] = ff; }}void dfs2(int u, int tp) { top[u] = tp; id[u] = ++num; if (son[u]) dfs2(son[u], tp); for (int i = 0; i < v[u].size(); i++) { int ff = v[u][i]; if (ff == fa[u] || ff == son[u]) continue; dfs2(ff, ff); }}struct Tree { int l,r,val;};Tree tree[4*N];void pushup(int x) { tree[x].val = max(tree[lson(x)].val, tree[rson(x)].val);}void build(int l,int r,int v) { tree[v].l=l; tree[v].r=r; if(l==r) { tree[v].val = val[l]; return ; } int mid=(l+r)>>1; build(l,mid,v*2); build(mid+1,r,v*2+1); pushup(v);}void update(int o,int v,int val) { //log(n) if(tree[o].l==tree[o].r) { tree[o].val = val; return ; } int mid = (tree[o].l+tree[o].r)/2; if(v<=mid) update(o*2,v,val); else update(o*2+1,v,val); pushup(o);}int query(int x,int l, int r) { if (tree[x].l >= l && tree[x].r <= r) { return tree[x].val; } int mid = (tree[x].l + tree[x].r) / 2; int ans = 0; if (l <= mid) ans = max(ans, query(lson(x),l,r)); if (r > mid) ans = max(ans, query(rson(x),l,r)); return ans;}int Yougth(int u, int v) { int tp1 = top[u], tp2 = top[v]; int ans = 0; while (tp1 != tp2) { if (dep[tp1] < dep[tp2]) { swap(tp1, tp2); swap(u, v); } ans = max(query(1,id[tp1], id[u]), ans); u = fa[tp1]; tp1 = top[u]; } if (u == v) return ans; if (dep[u] > dep[v]) swap(u, v); ans = max(query(1,id[son[u]], id[v]), ans); return ans;}void Clear(int n) { for(int i=1; i<=n; i++) v[i].clear();}int main() { int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); for(int i=1; i

 

 

转载于:https://www.cnblogs.com/jianrenfang/p/6354296.html

你可能感兴趣的文章
关于批处理-1
查看>>
Tomcat部署Web应用方法总结
查看>>
Python3 django2.0 字段加密 解密 AES
查看>>
CCNA实验之:网络地址转换(NAT)实验
查看>>
计算机网络原理笔记-停止等待协议
查看>>
确定当前记录和下一条记录之间相差的天数
查看>>
sql语句返回主键SCOPE_IDENTITY()
查看>>
机器学习开源项目精选TOP30
查看>>
/etc/resolv.conf文件详解
查看>>
【转】VC的MFC中重绘函数的使用总结(整理)
查看>>
JQuery日记_5.13 Sizzle选择器(六)选择器的效率
查看>>
oracle查看经常使用的系统信息
查看>>
Django_4_视图
查看>>
Linux的netstat命令使用
查看>>
lvm讲解,磁盘故障小案例
查看>>
大快网站:如何选择正确的hadoop版本
查看>>
经过这5大阶段,你离Java程序员就不远了!
查看>>
IntelliJ IDEA 连接数据库详细过程
查看>>
thymeleaf 学习笔记-基础篇
查看>>
PHP-X开发扩展
查看>>