1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm> #include<ctime> #include<iomanip> #include<queue> #include<stack> #include<map> #include<vector> #define gh() getchar() #define re register #define db double #define ls p<<1 #define rs p<<1|1 typedef long long ll; using namespace std; const int MAXN=1e5+1; const int INF=0x7f7f7f7f; template<class T> inline void read(T &x) { x=0; char ch=gh(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=gh(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=gh(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) { read(x),read(x1...); } template<class T> inline bool checkMax(T &x,T &y) { return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T &y) { return x>y?x=y,1:0; } int N; struct Graph { int next,to,val; Graph(int n=0,int t=0,int v=0):next(n),to(t),val(v){} }Edge[MAXN<<1]; int Head[MAXN],Total,Idx[MAXN],Bck[MAXN]; inline void addEdge(int u,int v,int w) { Edge[++Total]=Graph(Head[u],v,w);Head[u]=Total; Edge[++Total]=Graph(Head[v],u,w);Head[v]=Total; } int Fa[MAXN],Son[MAXN],Size[MAXN],Dep[MAXN],Path[MAXN]; void dfsTree(int x,int last) { Fa[x]=last,Size[x]=1,Dep[x]=Dep[last]+1; for(int e=Head[x],v;e;e=Edge[e].next) { if((v=Edge[e].to)==last) continue; Path[v]=Edge[e].val; dfsTree(v,x); Size[x]+=Size[v]; if(!Son[x]||Size[Son[x]]<Size[v]) Son[x]=v; } } int Top[MAXN],Dfn[MAXN],Val[MAXN],Cnt; void dfs_Seg(int x,int topf) { Dfn[x]=++Cnt; Val[Cnt]=Path[x],Top[x]=topf; if(!Son[x]) return ; dfs_Seg(Son[x],topf); for(int e=Head[x],v;e;e=Edge[e].next) { if((v=Edge[e].to)==Fa[x]||v==Son[x]) continue; dfs_Seg(v,v); } } struct SegmentTree { int l,r,val; }Tree[MAXN<<2]; inline void pushUp(int p) { Tree[p].val=max(Tree[ls].val,Tree[rs].val); } void build(int p,int l,int r) { Tree[p].l=l,Tree[p].r=r; if(l==r) { Tree[p].val=Val[l]; return ; } int mid=(l+r)>>1; build(ls,l,mid),build(rs,mid+1,r); pushUp(p); } void modify(int p,int d,int k) { if(d<=Tree[p].l&&Tree[p].r<=d) { Tree[p].val=k; return ; } int mid=(Tree[p].l+Tree[p].r)>>1; if(d<=mid) modify(ls,d,k); if(mid<d) modify(rs,d,k); pushUp(p); } int query(int p,int l,int r) { if(l<=Tree[p].l&&Tree[p].r<=r) return Tree[p].val; int val=-INF; int mid=(Tree[p].l+Tree[p].r)>>1; if(l<=mid) val=max(val,query(ls,l,r)); if(mid<r) val=max(val,query(rs,l,r)); return val; } inline int queryPath(int x,int y) { int res=-INF; while(Top[x]!=Top[y]) { if(Dep[Top[x]]<Dep[Top[y]]) swap(x,y); res=max(res,query(1,Dfn[Top[x]],Dfn[x])); x=Fa[Top[x]]; } if(Dep[x]>Dep[y]) swap(x,y); res=max(res,query(1,Dfn[x]+1,Dfn[y])); return res; } char Str[256]; int x,y; int main() { read(N); for(int i=2,u,v,w;i<=N;++i) { read(u,v,w); addEdge(u,v,w); Bck[i-1]=u,Idx[i-1]=v; } dfsTree(1,0); dfs_Seg(1,1); build(1,1,N); for(int i=1;i<N;++i) Idx[i]=(Bck[i]==Fa[Idx[i]]?Idx[i]:Bck[i]); scanf("%s",Str+1); while(Str[1]!='D') { read(x,y); if(Str[1]=='Q') { if(x==y) puts("0"); else printf("%d\n",queryPath(x,y)); } else modify(1,Dfn[Idx[x]],y); scanf("%s",Str+1); }
return 0; }
|