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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
#include <cstdio> #include <string> #include <cstring>
using namespace std; const int MAXN=10000+10; const int INF=0x3f3f3f3f;
struct { int next; int to; int cost; }node[MAXN*2]; int head[MAXN*2],tot; int n; int fa[MAXN],dep[MAXN],son[MAXN],top[MAXN],siz[MAXN],vis[MAXN]; int totw,w[MAXN];
void init(){ tot=0; memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); memset(fa,0,sizeof(fa)); memset(son,0,sizeof(son)); } void init2(){ memset(vis,0,sizeof(vis)); top[1]=1; totw=0; } void show(){ for(int i=1;i<=n;i++){ int pos=head[i]; printf("(%d) ",i); printf("fa:%d son:%d siz:%d dep:%d top:%d w:%d",fa[i],son[i],siz[i],dep[i],top[i],w[i]); for(;pos!=-1;pos=node[pos].next){ } printf("n"); } } void dfs1(int u,int d){ vis[u]=1; siz[u]=1; int pos=head[u]; int maxsize=0; int tmpson; for(;pos!=-1;pos=node[pos].next){ int v=node[pos].to; if(!vis[v]){ dfs1(v,d+1); siz[u]+=siz[v]; if(siz[v]>maxsize){ maxsize=siz[v]; tmpson=v; } fa[v]=u; } } if(maxsize){ son[u]=tmpson; } dep[u]=d; } void dfs2(int u){ int pos=head[u]; if(son[u]){ top[son[u]]=top[u]; w[son[u]]=++totw; dfs2(son[u]); } for(;pos!=-1;pos=node[pos].next){ int v=node[pos].to; if(v!=fa[u]&&v!=son[u]){ top[v]=v; w[v]=++totw; dfs2(v); } } }
int val[MAXN]; void updateval(){ for(int i=1;i<=n;i++){ int pos=head[i]; for(;pos!=-1;pos=node[pos].next){ int v=node[pos].to; if(v!=fa[i]){ val[w[v]]=node[pos].cost; } } } }
struct Treenode{ int l,r; int maxx; int minn; bool neg; }Tree[MAXN*4];
void push_up(int u){ int lson=u<<1; int rson=lson|1; int lmin,lmax; if(Tree[lson].neg){ lmin=-Tree[lson].maxx; lmax=-Tree[lson].minn; }else{ lmin=Tree[lson].minn; lmax=Tree[lson].maxx; } int rmin,rmax; if(Tree[rson].neg){ rmin=-Tree[rson].maxx; rmax=-Tree[rson].minn; }else{ rmin=Tree[rson].minn; rmax=Tree[rson].maxx; } Tree[u].maxx=max(lmax,rmax); Tree[u].minn=min(lmin,rmin); } void push_down(int u){ int lson=u<<1; int rson=lson|1; Tree[u].neg=!Tree[u].neg; Tree[lson].neg=!Tree[lson].neg; Tree[rson].neg=!Tree[rson].neg; push_up(u); } void build(int u,int l,int r){ Tree[u].l=l; Tree[u].r=r; Tree[u].maxx=-INF; Tree[u].minn=INF; Tree[u].neg=false; if(l==r){ Tree[u].maxx=val[l]; Tree[u].minn=val[l]; return; } int mid=(l+r)>>1; build((u<<1),l,mid); build((u<<1)|1,mid+1,r); push_up(u); } int query(int u,int l,int r){ if(Tree[u].l==l&&Tree[u].r==r){ if(!Tree[u].neg) return Tree[u].maxx; else return -Tree[u].minn; } if(Tree[u].neg) push_down(u); int mid=(Tree[u].l+Tree[u].r)>>1; int lson=u<<1; int rson=lson|1; if(l>mid){ return query(rson,l,r); }else if(r<=mid){ return query(lson,l,r); }else{ return max(query(lson,l,mid),query(rson,mid+1,r)); } } void update(int u,int k,int c){ if(Tree[u].neg) c=-c; if(Tree[u].l==k&&Tree[u].r==k){ Tree[u].maxx=c; Tree[u].minn=c; return; } if(Tree[u].neg) push_down(u); int mid=(Tree[u].l+Tree[u].r)>>1; if(k<=mid) update(u<<1,k,c); else update((u<<1)|1,k,c); push_up(u); } void neg_update(int u,int l,int r){ if(Tree[u].l==l&&Tree[u].r==r){ Tree[u].neg=!Tree[u].neg; return; } int mid=(Tree[u].l+Tree[u].r)>>1; if(r<=mid) neg_update(u<<1,l,r); else if(l>mid) neg_update((u<<1)|1,l,r); else{ neg_update(u<<1,l,mid); neg_update((u<<1)|1,mid+1,r); } push_up(u); } void neg_change(int u,int v){ int f1=top[u],f2=top[v]; while(f1!=f2){ if(dep[f1]<dep[f2]){ swap(f1,f2); swap(u,v); } neg_update(1,w[f1],w[u]); u=fa[f1],f1=top[u]; } if(u==v) return; if(dep[u]>dep[v]){ swap(u,v); } neg_update(1,w[son[u]],w[v]); }
int findmax(int u,int v){ int f1=top[u],f2=top[v]; int tmp=-INF; while(f1!=f2){ if(dep[f1]>dep[f2]){ tmp=max(tmp,query(1,w[f1],w[u])); u=fa[f1]; }else{ tmp=max(tmp,query(1,w[f2],w[v])); v=fa[f2]; } f1=top[u]; f2=top[v]; } if(u==v){ return tmp; } if(dep[u]>dep[v]){ swap(u,v); swap(f1,f2); } return max(tmp,query(1,w[son[u]],w[v])); } int main(){ freopen("in.txt","r",stdin); int T;cin>>T; while(T--){ init(); scanf("%d", &n); int a,b,c; for(int i=0;i<n-1;i++){ scanf("%d%d%d",&a,&b,&c); node[tot].to=b; node[tot].next=head[a]; node[tot].cost=c; head[a]=tot++; node[tot].to=a; node[tot].next=head[b]; node[tot].cost=c; head[b]=tot++; } dfs1(1,1); init2(); dfs2(1); updateval(); build(1,1,n); char str[10]; while(scanf("%s",str)!=EOF){ if(str[0]=='Q'){ scanf("%d%d",&a,&b); printf("%dn",findmax(a,b)); }else if(str[0]=='D'){ break; }else if(str[0]=='C'){ int k,tv; scanf("%d%d",&k,&tv); int ta=node[2*k-1].to; int tb=node[2*k-2].to; if(fa[ta]!=tb) swap(ta,tb); update(1,w[ta],tv); }else{ scanf("%d%d",&a,&b); neg_change(a,b); } } } return 0; }
|
近期评论