标签:
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:
The first line of input contains an integer t, the number of test cases (t <= 20). t test cases follow.
For each test case:
There is one blank line between successive tests.
For each "QUERY" operation, write one integer representing its result.
Input: 1 3 1 2 1 2 3 2 QUERY 1 2 CHANGE 1 3 QUERY 1 2 DONE Output: 13
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<iostream> #define max(a,b) (a>b?a:b) using namespace std; int head[50050],cnt,vis[50050]; struct s { int u,v,w,next; }edge[50050<<1]; struct LCT { int bef[50050],pre[50050],next[50050][2],key[50050],val[50050],belong[50050]; void init() { memset(pre,0,sizeof(pre)); memset(next,0,sizeof(next)); } void pushup(int x) { val[x]=max(key[x],max(val[next[x][1]],val[next[x][0]])); } void rotate(int x,int kind) { int y,z; y=pre[x]; z=pre[y]; next[y][!kind]=next[x][kind]; pre[next[x][kind]]=y; next[z][next[z][1]==y]=x; pre[x]=z; next[x][kind]=y; pre[y]=x; pushup(y); } void splay(int x) { int rt; for(rt=x;pre[rt];rt=pre[rt]); if(x!=rt) { bef[x]=bef[rt]; bef[rt]=0; while(pre[x]) { if(next[pre[x]][0]==x) { rotate(x,1); } else rotate(x,0); } pushup(x); } } void access(int x) { int fa; for(fa=0;x;x=bef[x]) { splay(x); pre[next[x][1]]=0; bef[next[x][1]]=x; next[x][1]=fa; pre[fa]=x; bef[fa]=0; fa=x; pushup(x); } } void change(int x,int val) { int t; t=belong[x-1]; key[t]=val; splay(t); } int query(int x,int y) { access(y); for(y=0;x;x=bef[x]) { splay(x); if(!bef[x]) return max(val[y],val[next[x][1]]); pre[next[x][1]]=0; bef[next[x][1]]=x; next[x][1]=y; pre[y]=x; bef[y]=0; y=x; pushup(x); } } }lct; void add(int u,int v,int w) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; } void bfs(int u) { int i,y; queue<int>q; memset(vis,0,sizeof(vis)); vis[u]=1; q.push(u); while(!q.empty()) { u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!vis[v]) { lct.bef[v]=u; lct.key[v]=lct.val[v]=edge[i].w; lct.belong[i>>1]=v; vis[v]=1; q.push(v); } } } } int main() { int t; scanf("%d",&t); while(t--) { int n; memset(head,-1,sizeof(head)); cnt=0; int i; char s[10]; scanf("%d",&n); for(i=1;i<n;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); add(a,b,c); add(b,a,c); } lct.init(); bfs(1); while(scanf("%s",s)!=EOF) { int a,b; if(strcmp(s,"DONE")==0) break; scanf("%d%d",&a,&b); if(s[0]=='Q') { printf("%d\n",lct.query(a,b)); } else lct.change(a,b); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
SPOJ 题目375 Query on a tree(link cut tree边权更新,求两点最大值)
标签:
原文地址:http://blog.csdn.net/yu_ch_sh/article/details/47729115