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
| /*
hdu4010
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
template<typename T>
void Swap(T& a,T& b){T t=a;a=b;b=t;}
template<typename T>
bool read(T& w)
{
char r;int f=1;
for(r=getchar();r!=-1&&r!='-'&&(r<48||r>57);r=getchar());
if(r=='-')r=getchar(),f=-1;
if(r==-1)return false;
for(w=0;r>=48&&r<=57;r=getchar())w=w*10+r-48;
w*=f;
return true;
}
template<typename T>
void write(T w,char c=0)
{
if(w<0)w=-w,putchar('-');
if(w>9)write(w/10);
putchar(w%10+48);
if(c)putchar(c);
}
template<typename T>
T Max(const T& a,const T& b){return a>b?a:b;}
const int maxn=300003;
const int inf=2147483647;
int n,q;
//Splay
int f[maxn],c[maxn][2],maxv[maxn],w[maxn];
bool rev[maxn];
int add[maxn];
#define lc(k) c[k][0]
#define rc(k) c[k][1]
bool isroot(int k)
{
return lc(f[k])!=k&&rc(f[k])!=k;
}
void update(int k)
{
maxv[k]=Max(Max(maxv[lc(k)],maxv[rc(k)]),w[k]);
}
void tagdown(int k)
{
if(rev[k])
{
rev[k]^=1,rev[lc(k)]^=1,rev[rc(k)]^=1;
Swap(lc(k),rc(k));
}
if(add[k])
{
if(lc(k))add[lc(k)]+=add[k],maxv[lc(k)]+=add[k],w[lc(k)]+=add[k];
if(rc(k))add[rc(k)]+=add[k],maxv[rc(k)]+=add[k],w[rc(k)]+=add[k];
add[k]=0;
}
}
void rotate(int x,int& k)
{
int y=f[x],z=f[y];
int l=int(x!=lc(y));
int r=l^1;
if(y==k)k=x;
else
{
if(y==lc(z))lc(z)=x;
else rc(z)=x;
}
f[x]=z,f[y]=x,f[c[x][r]]=y;
c[y][l]=c[x][r],c[x][r]=y;
update(y),update(x);
}
void splay(int x,int& k)
{
while(x!=k)
{
int y=f[x],z=f[y];
if(y!=k)
{
if(x==lc(y) ^ y==lc(z))
rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
int stk[maxn],top=0;
void Splay(int x)
{
int rt;
for(rt=x;!isroot(rt);rt=f[rt])
stk[++top]=rt;
for(tagdown(rt);top;--top)tagdown(stk[top]);
splay(x,rt);
}
void access(int x)
{
for(int t=0;x;)
{
Splay(x);
rc(x)=t;
update(x);
t=x;x=f[x];
}
}
void mroot(int x)
{
access(x),Splay(x),rev[x]^=1;
}
void link(int x,int y)
{
mroot(x),f[x]=y;
//Splay(x);
}
void cut(int x,int y)
{
mroot(x),access(y),Splay(y),lc(y)=f[lc(y)]=0;//should not used lc(y)=f[x]=0, because we should cut y and its parent
update(y);
}
struct EDGE
{
int to,nxt;
void init(int too,int nxtt)
{
to=too,nxt=nxtt;
}
}edge[maxn<<1];
int ek=0;
int head[maxn];
void addEdge(int k,int v)
{
edge[++ek].init(v,head[k]);
head[k]=ek;
}
void dfs(int k)
{
for(int i=head[k];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(v!=f[k])
{
f[v]=k;
dfs(v);
}
}
}
void Init()
{
ek=0;
maxv[0]=-inf;
for(int i=0;i<=n;++i)
{
f[i]=c[i][0]=c[i][1]=0;
rev[i]=false;
add[i]=0;
head[i]=0;
}
}
void input()
{
for(int i=1,x,y;i<=n-1;++i)
{
read(x),read(y);
addEdge(x,y);
addEdge(y,x);
}
for(int i=1;i<=n;++i)
{
read(w[i]);
maxv[i]=w[i];
}
dfs(1);
}
int findroot(int x)
{
access(x),Splay(x);
while(lc(x))x=lc(x);
return x;
}
inline bool insame(int x,int y)
{
return findroot(x)==findroot(y);
}
void solve()
{
int cmd,x,y,ww;
bool illegal;
for(read(q);q;--q)
{
illegal=false;
read(cmd);
if(cmd==1)
{
read(x),read(y);
if(insame(x,y))illegal=true;
else link(x,y);
}
else if(cmd==2)
{
read(x),read(y);
if(x==y||!insame(x,y))illegal=true;
else cut(x,y);
}
else if(cmd==3)
{
read(ww),read(x),read(y);
if(!insame(x,y))illegal=true;
else
{
mroot(x);
access(y),Splay(y);
w[y]+=ww,maxv[y]+=ww,add[y]+=ww;
}
}
else if(cmd==4)
{
read(x),read(y);
if(!insame(x,y))illegal=true;
else
{
mroot(x);
access(y),Splay(y);
write(maxv[y],'\n');
}
}
if(illegal)puts("-1");
}
}
int main()
{
freopen("hdu4010_1.in","r",stdin);
//freopen("hdu4010.out","w",stdout);
while(read(n))
{
Init();
input();
solve();
putchar('\n');
}
return 0;
}
|