Fzu Problem 2082 过路费 LCT,动态树

Problem 2082 过路费

Accept: 528    Submit: 1654
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Fzu Problem 2082 过路费  LCT,动态树 Problem Description

有n座城市,由n-1条路相连通,使得任意两座城市之间可达。每条路有过路费,要交过路费才能通过。每条路的过路费经常会更新,现问你,当前情况下,从城市a到城市b最少要花多少过路费。

Fzu Problem 2082 过路费  LCT,动态树 Input

有多组样例,每组样例第一行输入两个正整数n,m(2 <= n<=50000,1<=m <= 50000),接下来n-1行,每行3个正整数a b c,(1 <= a,b <= n , a != b , 1 <= c <= 1000000000).数据保证给的路使得任意两座城市互相可达。接下来输入m行,表示m个操作,操作有两种:一. 0 a b,表示更新第a条路的过路费为b,1 <= a <= n-1 ; 二. 1 a b , 表示询问a到b最少要花多少过路费。

Fzu Problem 2082 过路费  LCT,动态树 Output

对于每个询问,输出一行,表示最少要花的过路费。

Fzu Problem 2082 过路费  LCT,动态树 Sample Input

2 3
1 2 1
1 1 2
0 1 2
1 2 1

Fzu Problem 2082 过路费  LCT,动态树 Sample Output

1
2

Fzu Problem 2082 过路费  LCT,动态树 Source

FOJ有奖月赛-2012年4月(校赛热身赛)

 
 
题解:
直接动态树维护树上两点间的和即可。。。
注意开long long。。。
 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long
#define MAXN 50010
struct node
{
int left,right,val;
LL sum;
}tree[MAXN*];
int father[MAXN*],Stack[*MAXN],rev[*MAXN];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(fh=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
int isroot(int x)
{
return tree[father[x]].left!=x&&tree[father[x]].right!=x;
}
void pushdown(int x)
{
int l=tree[x].left,r=tree[x].right;
if(rev[x]!=)
{
rev[x]^=;rev[l]^=;rev[r]^=;
swap(tree[x].left,tree[x].right);
}
}
void Pushup(int x)
{
int l=tree[x].left,r=tree[x].right;
tree[x].sum=tree[l].sum+tree[r].sum+tree[x].val;
}
void rotate(int x)
{
int y=father[x],z=father[y];
if(!isroot(y))
{
if(tree[z].left==y)tree[z].left=x;
else tree[z].right=x;
}
if(tree[y].left==x)
{
father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y;
}
else
{
father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y;
}
Pushup(y);Pushup(x);
}
void splay(int x)
{
int top=,i,y,z;Stack[++top]=x;
for(i=x;!isroot(i);i=father[i])Stack[++top]=father[i];
for(i=top;i>=;i--)pushdown(Stack[i]);
while(!isroot(x))
{
y=father[x];z=father[y];
if(!isroot(y))
{
if((tree[y].left==x)^(tree[z].left==y))rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int last=;
while(x!=)
{
splay(x);
tree[x].right=last;Pushup(x);
last=x;x=father[x];
}
}
void makeroot(int x)
{
access(x);splay(x);rev[x]^=;
}
void link(int u,int v)
{
makeroot(u);father[u]=v;splay(u);
}
void cut(int u,int v)
{
makeroot(u);access(v);splay(v);father[u]=tree[v].left=;
}
int findroot(int x)
{
access(x);splay(x);
while(tree[x].left!=)x=tree[x].left;
return x;
}
int main()
{
int n,m,i,fh,aa,bb,cc;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(i=;i<=*n;i++)tree[i].sum=tree[i].left=tree[i].right=tree[i].val=father[i]=rev[i]=;
for(i=;i<n;i++)
{
aa=read();bb=read();cc=read();
tree[n+i].val=cc;
link(aa,n+i);link(n+i,bb);
}
for(i=;i<=m;i++)
{
fh=read();aa=read();bb=read();
if(fh==)
{
splay(n+aa);
tree[n+aa].val=bb;
}
else
{
makeroot(aa);access(bb);splay(bb);
printf("%lld\n",tree[bb].sum);
}
}
}
return ;
}
上一篇:FZu Problem 2236 第十四个目标 (线段树 + dp)


下一篇:学习笔记:Java的一些基础小知识之JVM与GC