标签:
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 7639 | Accepted: 1990 |
Description
Input
Output
Sample Input
3 3 1 1 2 1 2 3 2 0 2 1 2 3 0 3
Sample Output
1 3
Source
POJ Monthly--2006.02.26,zgl & twb
题目大意:操作 0 查询当前点到这个点的最短距离,操作1修改第k个边的权值
Problem: 2763 User: kxh1995 Memory: 7636K Time: 1141MS Language: C++ Result: Accepted
ac代码
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<iostream> using namespace std; int head[100010],cnt,vis[100050]; struct s { int u,v,w,next; }edge[150050<<1]; struct LCT { int bef[150050],pre[150050],next[150050][2],key[150050],sum[150050],belong[150050]; void init() { memset(pre,0,sizeof(pre)); memset(next,0,sizeof(next)); } void pushup(int x) { sum[x]=key[x]+sum[next[x][1]]+sum[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 sum[y]+sum[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.sum[v]=edge[i].w; lct.belong[i>>1]=v; vis[v]=1; q.push(v); } } } } int main() { int n,m,s; while(scanf("%d%d%d",&n,&m,&s)!=EOF) { int i; lct.init(); cnt=0; memset(head,-1,sizeof(head)); for(i=1;i<n;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } bfs(1); while(m--) { int op; scanf("%d",&op); if(!op) { int x; scanf("%d",&x); printf("%d\n",lct.query(s,x)); s=x; } else { int x,y; scanf("%d%d",&x,&y); lct.change(x,y); } } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDOJ 题目2763 Housewife Wind(Link Cut Tree修改边权,查询两点间距离)
标签:
原文地址:http://blog.csdn.net/yu_ch_sh/article/details/47984881