Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 7479 | Accepted: 1938 |
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
也可以修改某条边的边权。
分析:
对每个点建立一个结点,值为0
对每个边建立一个结点,值为边权。
然后用LCT维护即可
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> using namespace std; #define maxn 500007 #define inf 1000000000 #define ll int struct Node{ Node *fa,*ch[2]; bool rev,root; int val; ll minv; }; Node pool[maxn]; Node *nil,*tree[maxn]; int cnt = 0; void init(){ cnt = 1; nil = tree[0] = pool; nil->ch[0] = nil->ch[1] = nil; nil->val = 0; nil->minv = 0; } Node *newnode(int val,Node *f){ pool[cnt].fa = f; pool[cnt].ch[0]=pool[cnt].ch[1]=nil; pool[cnt].rev = false; pool[cnt].root = true; pool[cnt].val = val; pool[cnt].minv = val; return &pool[cnt++]; } //左右子树反转******真正把结点变为根 void update_rev(Node *x){ if(x == nil) return ; x->rev = !x->rev; swap(x->ch[0],x->ch[1]); } //splay向上更新信息****** void update(Node *x){ if(x == nil) return ; x->minv = x->val; if(x->ch[0] != nil) x->minv += x->ch[0]->minv; if(x->ch[1] != nil) x->minv += x->ch[1]->minv; } //splay下推信息****** void pushdown(Node *x){ if(x->rev != false){ update_rev(x->ch[0]); update_rev(x->ch[1]); x->rev = false; } } //splay在root-->x的路径下推信息****** void push(Node *x){ if(!x->root) push(x->fa); pushdown(x); } //将结点x旋转至splay中父亲的位置****** void rotate(Node *x){ Node *f = x->fa, *ff = f->fa; int t = (f->ch[1] == x); if(f->root) x->root = true, f->root = false; else ff->ch[ff->ch[1] == f] = x; x->fa = ff; f->ch[t] = x->ch[t^1]; x->ch[t^1]->fa = f; x->ch[t^1] = f; f->fa = x; update(f); } //将结点x旋转至x所在splay的根位置****** void splay(Node *x){ push(x); Node *f, *ff; while(!x->root){ f = x->fa,ff = f->fa; if(!f->root) if((ff->ch[1]==f)&&(f->ch[1] == x)) rotate(f); else rotate(x); rotate(x); } update(x); } //将x到树根的路径并成一条path****** Node *access(Node *x){ Node *y = nil,*z; while(x != nil){ splay(x); x->ch[1]->root = true; (x->ch[1] = y)->root = false; update(x); y = x; x = x->fa; } return y; } //将结点x变成树根****** void be_root(Node *x){ access(x); splay(x); update_rev(x); } //将x连接到结点f上****** void link(Node *x, Node *f){ be_root(x); x->fa = f; } Node * find(Node *root){ if(root->ch[0] == nil) return root; return find(root->ch[0]); } int ru[maxn],rv[maxn]; int main(){ int n,q,s; Node*x,*y,*z; while(scanf("%d%d%d",&n,&q,&s)!=EOF){ init(); int u,v,t; for(int i = 1;i <= n; i++) tree[i] = newnode(0,nil); for(int i = 1;i < n ;i++){ scanf("%d%d%d",&u,&v,&t); x = newnode(t,nil); link(tree[u],x); link(tree[v],x); ru[i] = u; rv[i] = v; } while(q--){ scanf("%d",&t); if(t == 0){ scanf("%d",&u); be_root(tree[s]); y = access(tree[u]); printf("%d\n",y->minv); s = u; } else { scanf("%d%d",&u,&v); be_root(tree[ru[u]]); access(tree[rv[u]]); splay(tree[ru[u]]); y = find(tree[ru[u]]->ch[1]); y->val = v; update(y); splay(y); } } } return 0; } /* 3 100 1 1 2 1 2 3 2 0 2 1 2 3 0 3 1 2 5 0 2 */