您的位置:首页 > 产品设计 > UI/UE

动态树(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 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:
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;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: