# bzoj3091

（居然真是小学生都会。。

（简直不如07年水平=.=

popoqqq大爷讲的好

#include <cstdio>
#define LL long long
int n,m;

int ch[50010][2],rv[50010],fa[50010];
int x[50010],lz[50010],sz[50010];
LL sum[50010],lsum[50010],rsum[50010],exp[50010];

int st[50010],tp;

char c = getchar();
int re = 0;
for(;c > '9' || c < '0';c = getchar());
for(;c >= '0' && c <= '9';c = getchar())
re = re * 10 + c - '0';
return re;
}

void pu (int u) {
sum[u] = (LL)x[u] + sum[ch[u][0]] + sum[ch[u][1]];
sz[u] = sz[ch[u][0]] + sz[ch[u][1]] + 1;

lsum[u] = lsum[ch[u][0]] + (LL)(sz[ch[u][0]] + 1) * (LL)x[u] + lsum[ch[u][1]] + (LL)(sz[ch[u][0]] + 1) * sum[ch[u][1]];
rsum[u] = rsum[ch[u][1]] + (LL)(sz[ch[u][1]] + 1) * (LL)x[u] + rsum[ch[u][0]] + (LL)(sz[ch[u][1]] + 1) * sum[ch[u][0]];
exp[u] = (LL)(sz[ch[u][1]] + 1) * lsum[ch[u][0]] + exp[ch[u][0]] + (LL)(sz[ch[u][0]] + 1) * (LL)(sz[ch[u][1]] + 1) * (LL)x[u] + (LL)(sz[ch[u][0]] + 1) * rsum[ch[u][1]] + exp[ch[u][1]];
}

void reverse (int u) {
int t = ch[u][0];
ch[u][0] = ch[u][1];
ch[u][1] = t;
LL tt = lsum[u];
lsum[u] = rsum[u];
rsum[u] = tt;
rv[u] ^= 1;
}

void add (int u,int v) {
x[u] += v;
sum[u] += sz[u] * v;
lsum[u] += (LL)(sz[u] + 1) * (LL)sz[u] * (LL)v / 2;
rsum[u] += (LL)(sz[u] + 1) * (LL)sz[u] * (LL)v / 2;
exp[u] += v * ((LL)sz[u] * (LL)(sz[u] + 1) * (LL)(sz[u] + 2) / 6LL);
lz[u] += v;
}

void pd (int u) {
if(rv[u]) {
rv[u] = 0;
reverse(ch[u][0]);
reverse(ch[u][1]);
}
if(lz[u]) {
lz[u] = 0;
}
}

void rot (int u,int d) {
ch[fa[u]][d ^ 1] = ch[u][d];
if(ch[u][d])
fa[ch[u][d]] = fa[u];
int t = fa[fa[u]];
ch[u][d] = fa[u];
fa[fa[u]] = u;
fa[u] = t;

pu(ch[u][d]);
pu(u);

if(!t)
return;
if(ch[t][0] == ch[u][d])
ch[t][0] = u;
if(ch[t][1] == ch[u][d])
ch[t][1] = u;
}

void splay (int u) {
int t = u;
st[tp = 1] = t;
while(ch[fa[t]][0] == t || ch[fa[t]][1] == t) {
t = fa[t];
st[++tp] = t;
}
while(tp)
pd(st[tp--]);

while(ch[fa[u]][0] == u || ch[fa[u]][1] == u) {
if(ch[fa[fa[u]]][0] != fa[u] && ch[fa[fa[u]]][1] != fa[u]) {
if(ch[fa[u]][0] == u)
rot(u,1);
else rot(u,0);
}
else {
int d = ch[fa[fa[u]]][0] == fa[u];
if(ch[fa[u]][d] == u) {
rot(u,d ^ 1);
rot(u,d);
}
else {
rot(u,d);
rot(u,d);
}
}
}
}

void access (int u) {
int v = 0;
while(u) {
splay(u);
ch[u][1] = v;
pu(u);
v = u;
u = fa[u];
}
}

void mtr (int u) {
access(u);
splay(u);
reverse(u);
}

int fr (int u) {
access(u);
splay(u);
int t = u;
while(ch[t][0])
t = ch[t][0];
return t;
}

LL gcd (LL a,LL b) {
if(a % b)
return gcd(b,a % b);
return b;
}

int main () {

for(int i = 1;i <= n;++i) {
exp[i] = lsum[i] = rsum[i] = x[i] = sum[i] = read_int();
sz[i] = 1;
}

int f,u,v,t;
for(int i = 1;i < n;++i) {
mtr(u);
mtr(v);
fa[u] = v;
}

for(int i = 1;i <= m;++i) {
if(f == 1) {
if(fr(u) != fr(v))
continue;
mtr(u);
access(v);
splay(u);
if(ch[u][1] == v && !ch[v][0]) {
access(u);
fa[v] = 0;
}
}
else {
if(f == 2) {
if(fr(u) == fr(v))
continue;
mtr(u);
mtr(v);
fa[v] = u;
}
else {
if(f == 3) {
if(fr(u) != fr(v))
continue;
mtr(u);
access(v);
splay(u);
}
else {
if(fr(u) != fr(v))
printf("-1\n");
else {
mtr(u);
access(v);
splay(u);
LL hh = gcd(exp[u],(LL)(sz[u] + 1) * (LL)sz[u] / 2LL);
LL ff = exp[u] / hh;
hh = (LL)(sz[u] + 1) * (LL)sz[u] / 2LL / hh;
printf("%lld/%lld\n",ff,hh);
}
}
}
}
}
}