#include<bits/stdc++.h>
#include<windows.h>
#define int long long
#define lf long double
using namespace std;
signed main(){
int T=100,i,cnt;
double st,en;
while(T--){
system("Rand.exe");
st=clock();
system("Mine.exe");
en=clock();
system("Others.exe");
if(system("fc yl.ans yl.out")){
cout<<"WA";
return 0;
}
if(en-st>=1000){
printf("#%d:TLE 用时:%lf ms\n",++i,en-st);
}
else printf("#%d:AC 用时:%lf ms\n",++i,en-st),cnt++;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, m, tot, k, ans;
string s[5] = {"", "Max", "Cover", "Add", "Change"};
int random(int n) {
return 1ll * rand() * rand() % n + 1;
}
int main() {
freopen("yl.in","w",stdout);
srand((unsigned)time(NULL));
n = random(30) + 1;
printf("%d\n", n);
for(int i = 2; i <= n; i++) {
int fa = random(i - 1);
int z = random(1e9);
printf("%d %d %d\n", i, fa, z);
}
for(int i = 1; i <= 10000; i++) {
int p = random(4);
cout << s[p] << " ";
if(s[p][0] == 'M') {
int x = random(n), y = random(n);
while(x == y) {
x = random(n);
y = random(n);
}
printf("%d %d\n", x, y);
}
else if(s[p][1] == 'o') {
int x = random(n), y = random(n), z = random(1e9);
while(x == y) {
x = random(n);
y = random(n);
}
printf("%d %d %d\n", x, y, z);
}
else if(s[p][1] == 'h') {
int x = random(n - 1), z = random(1e9);
printf("%d %d\n", x, z);
}
else if(s[p][1] == 'd') {
int x = random(n), y = random(n), z = random(10);
while(x == y) {
x = random(n);
y = random(n);
}
printf("%d %d %d\n", x, y, z);
}
}
puts("Stop");
return 0;
}
#include<bits/stdc++.h>
#define FOR(i,u) for(ll i=G.head[u];i;i=G.nex[i])
#define lc (d<<1)
#define rc (d<<1|1)
#define mid (l+r>>1)
#define ll long long
#define C const
using namespace std;
void read(ll &x) {
x=0;
char y=getchar();
bool flag=0;
while(y<'0' || y>'9') {
if(y=='-')flag=1;
y=getchar();
}
while((y>='0' && y<='9')) {
x=(x<<1)+(x<<3)+(y-'0');
y=getchar();
}
if(flag)x=-x;
}
void write(ll x) {
if(x<0) {
putchar('-');
x=-x;
}
if(x<10)putchar(x+'0');
else {
write(x/10);
putchar(x%10+'0');
}
}
C ll N=1e5+5;
#define For(i,u,G) for(ll i=G.head[u];i;i=G.nex[i])
struct my_edge{
ll head[N],nex[N<<1],from[N<<1],to[N<<1],len[N<<1],size;
void push(ll u,ll v,ll w){
nex[++size]=head[u];
from[size]=u;
to[size]=v;
len[size]=w;
head[u]=size;
}
void clear(){
size=0;
memset(head,0,sizeof head);
}
}G;
ll n,u,v,w,k,fa[N],h[N],siz[N],top[N],son[N],dfn[N],fn[N],fw[N],tot;
void dfs1(ll u,ll f){
fa[u]=f;
siz[u]=1;
h[u]=h[f]+1;
ll v,maxid=0;
For(i,u,G){
v=G.to[i];
if(v==f)continue;
fw[v]=G.len[i];
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[maxid])maxid=v;
}
son[u]=maxid;
}
void dfs2(ll u,ll f){
top[u]=f;
dfn[u]=++tot;
fn[tot]=u;
if(son[u]==0)return;
dfs2(son[u],f);
ll v;
For(i,u,G){
v=G.to[i];
if(v==fa[u] || v==son[u])continue;
dfs2(v,v);
}
}
struct my_segment_tree{
ll sum[4*N],lb1[4*N],lb2[4*N];
void Up(C ll &d){
sum[d]=max(sum[lc],sum[rc]);
}
void Down(C ll &d,C ll &l,C ll &r){
if(lb2[d]!=-1){
sum[lc]=lb2[d];
sum[rc]=lb2[d];
lb2[lc]=lb2[d];
lb2[rc]=lb2[d];
lb2[d]=-1;
}
if(lb1[d]){
sum[lc]+=lb1[d];
sum[rc]+=lb1[d];
lb1[lc]+=lb1[d];
lb1[rc]+=lb1[d];
lb1[d]=0;
}
}
void build(C ll &d,C ll &l,C ll &r){
lb2[d]=-1;
if(l==r){
sum[d]=fw[fn[l]];
return;
}
build(lc,l,mid);
build(rc,mid+1,r);
Up(d);
}
void change(C ll &d,C ll &l,C ll &r,C ll &x,C ll &y,C ll &k){
if(y<x)return;
if(r<x || y<l)return;
if(x<=l && r<=y){
sum[d]=k;
lb2[d]=k;
lb1[d]=0;
return;
}
Down(d,l,r);
change(lc,l,mid,x,y,k);
change(rc,mid+1,r,x,y,k);
Up(d);
}
void add(C ll &d,C ll &l,C ll &r,C ll &x,C ll &y,C ll &k){
if(y<x)return;
if(r<x || y<l)return;
if(x<=l && r<=y){
sum[d]+=k;
lb1[d]+=k;
return;
}
Down(d,l,r);
add(lc,l,mid,x,y,k);
add(rc,mid+1,r,x,y,k);
Up(d);
}
ll query(C ll &d,C ll &l,C ll &r,C ll &x,C ll &y){
if(y<x)return -1e9;
if(r<x || y<l)return -1e9;
if(x<=l && r<=y)return sum[d];
Down(d,l,r);
return max(query(lc,l,mid,x,y),query(rc,mid+1,r,x,y));
}
}tree;
string op;
ll LCA(ll u,ll v,ll op){
if(op==1){
ll maxn=-1e9;
while(top[u]!=top[v]){
if(h[top[v]]<h[top[u]])swap(u,v);
maxn=max(maxn,tree.query(1,1,n,dfn[top[v]],dfn[v]));
v=fa[top[v]];
}
if(h[v]<h[u])maxn=max(maxn,tree.query(1,1,n,dfn[v]+1,dfn[u]));
else maxn=max(maxn,tree.query(1,1,n,dfn[u]+1,dfn[v]));
return maxn;
}else if(op==2){
while(top[u]!=top[v]){
if(h[top[v]]<h[top[u]])swap(u,v);
tree.change(1,1,n,dfn[top[v]],dfn[v],w);
v=fa[top[v]];
}
if(h[v]<h[u])tree.change(1,1,n,dfn[v]+1,dfn[u],w);
else tree.change(1,1,n,dfn[u]+1,dfn[v],w);
}else{
while(top[u]!=top[v]){
if(h[top[v]]<h[top[u]])swap(u,v);
tree.add(1,1,n,dfn[top[v]],dfn[v],w);
v=fa[top[v]];
}
if(h[v]<h[u])tree.add(1,1,n,dfn[v]+1,dfn[u],w);
else tree.add(1,1,n,dfn[u]+1,dfn[v],w);
}
return 0;
}
signed main() {
freopen("yl.in","r", stdin);
freopen("yl.out","w", stdout);
read(n);
for(ll i=1;i<n;i++){
read(u);read(v);read(w);
G.push(u,v,w);
G.push(v,u,w);
}
dfs1(1,0);
dfs2(1,1);
tree.build(1,1,n);
cin>>op;
while(op!="Stop"){
if(op=="Max"){
read(u);
read(v);
write(LCA(u,v,1));
puts("");
}else if(op=="Change"){
read(k);
read(w);
u=G.from[k<<1];
v=G.to[k<<1];
if(fa[u]==v)u=dfn[u];
else u=dfn[v];
tree.change(1,1,n,u,u,w);
}else if(op=="Cover"){
read(u);
read(v);
read(w);
LCA(u,v,2);
}else{
read(u);
read(v);
read(w);
LCA(u,v,3);
}
cin>>op;
}
return 0;
}