SPOJ 375 树链剖分
来源:互联网 发布:vc2010编程实例教程 编辑:程序博客网 时间:2024/05/21 21:37
SPOJ Problem Set (classical)
375. Query on a tree
Problem code: QTREE
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 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:131 2 12 3 2QUERY 1 2CHANGE 1 3QUERY 1 2DONEOutput:13
边权,单点修改,区间最值。
代码:
/* ***********************************************Author :xianxingwuguanCreated Time :2014/3/1 10:06:18File Name :1.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;const int maxn=60010;struct Edge{int next,to;}edge[2*maxn];int head[maxn],tot;int top[maxn];int fa[maxn];int deep[maxn];int num[maxn];int p[maxn],fp[maxn],son[maxn],pos;void init(){tot=0;memset(head,-1,sizeof(head));pos=0;memset(son,-1,sizeof(son));}void addedge(int u,int v){edge[tot].to=v;edge[tot].next=head[u];head[u]=tot++;}void dfs1(int u,int pre,int d){deep[u]=d;fa[u]=pre;num[u]=1;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(v==pre)continue;dfs1(v,u,d+1);num[u]+=num[v];if(son[u]==-1||num[v]>num[son[u]])son[u]=v;}}void getpos(int u,int sp){top[u]=sp;p[u]=pos++;fp[p[u]]=u;if(son[u]==-1)return;getpos(son[u],sp);for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(v!=son[u]&&v!=fa[u])getpos(v,v);}}struct Node{int l,r,sum;}tree[7*maxn];void build(int i,int l,int r){tree[i].l=l;tree[i].r=r;tree[i].sum=0;if(l==r)return;int mid=(l+r)/2;build(2*i,l,mid);build(2*i+1,mid+1,r);}void pushup(int i){tree[i].sum=max(tree[2*i].sum,tree[2*i+1].sum);}void update(int i,int k,int val){if(tree[i].l==tree[i].r){tree[i].sum=val;return;}int mid=(tree[i].l+tree[i].r)>>1;if(k<=mid)update(2*i,k,val);else update(2*i+1,k,val);pushup(i);}int query(int i,int l,int r){if(tree[i].l>=l&&tree[i].r<=r)return tree[i].sum;int mid=(tree[i].l+tree[i].r)/2;int ans=0;if(l<=mid)ans=max(ans,query(2*i,l,r));if(r>mid)ans=max(ans,query(2*i+1,l,r));return ans;}int find(int u,int v){int f1=top[u],f2=top[v];int tmp=0;while(f1!=f2){if(deep[f1]<deep[f2])swap(f1,f2),swap(u,v);tmp=max(tmp,query(1,p[f1],p[u]));u=fa[f1];f1=top[u];}if(u==v)return tmp;if(deep[u]>deep[v])swap(u,v);return max(tmp,query(1,p[son[u]],p[v]));}int e[maxn][3];int main(){ //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int n,m,T; scanf("%d",&T); while(T--){ init(); scanf("%d",&n); for(int i=0;i<n-1;i++){ scanf("%d%d%d",&e[i][0],&e[i][1],&e[i][2]); addedge(e[i][0],e[i][1]); addedge(e[i][1],e[i][0]); } dfs1(1,0,0); getpos(1,1);// cout<<"111111"<<endl; build(1,0,pos-1);// cout<<"222222"<<endl; for(int i=0;i<n-1;i++){ if(deep[e[i][0]]>deep[e[i][1]]) swap(e[i][0],e[i][1]); update(1,p[e[i][1]],e[i][2]); } char op[33]; while(~scanf("%s",op)){ int a,b; if(op[0]=='D')break; scanf("%d%d",&a,&b); if(op[0]=='C')update(1,p[e[a-1][1]],b); else printf("%d\n",find(a,b)); } } return 0;}
0 0
- spoj 375 (树链剖分)
- SPOJ 375 树链剖分
- 【转】SPOJ 375 树链剖分
- spoj 375 树链剖分模板
- SPOJ 375 树链剖分学习
- SPOJ 375 树链剖分
- SPOJ 375 树链剖分
- SPOJ 375 树链剖分
- SPOJ 375 树链剖分
- 树链剖分模板(spoj 375)
- 树链剖分(SPOJ 375)
- LCT spoj 375 树链剖分
- SPOJ 375 (树链剖分+线段树)
- SPOJ 375 (树链剖分+线段树)
- Spoj 375 QTREE(树链剖分)
- spoj 375
- [SPOJ 375]Query On a Tree(树链剖分)
- Spoj 375 Qtree 树链剖分 + 线段树 解法
- FZU 2082 树链剖分
- MOGRE学习笔记(1) - OGRE简介及在vs2010下配置
- C/C++ 调试技巧
- Confluence 空间附件(Attachments )宏
- 开发之余实用小技巧
- SPOJ 375 树链剖分
- Android开源项目发现---ImageView 篇(持续更新)
- Android开源项目发现---ProgressBar 篇(持续更新)
- 让linux下无线网卡变身随身wifi----iptables移植--结束
- Windows Server 2003远程桌面连接注销和结束会话
- [Mysql]重置auto_increment初始值
- 特殊回文数
- Android开源项目发现---Layout 篇(持续更新)
- Spring MVC和Struts2的比较