5 n
,tot
,edge_tot
,p
,q
,t
,i
:longint;
6 dep
,top
,father
,son
,w
,size
,ehead
:array [0..maxn
] of longint;
7 edge
:array [0..maxn
*2] of record
10 tree
:array [0..maxn
*4] of longint;
11 data
:array [0..maxn
] of record
16 //Implementation of Segment Tree
18 function max(p
,q
:longint):longint;
20 if p
>q
then exit(p
) else exit(q
);
23 procedure update(root
:longint);
25 tree
[root
]:=max(tree
[root
*2],tree
[root
*2+1]);
28 procedure tree_ins(l
,r
,pos
,v
,root
:longint);
39 if pos
<=m
then tree_ins(l
,m
,pos
,v
,root
*2) else
40 tree_ins(m
+1,r
,pos
,v
,root
*2+1);
44 function tree_query(l
,r
,p
,q
,root
:longint):longint;
49 if (p
<=l
) and (r
<=q
) then exit(tree
[root
]);
52 if p
<=m
then t
:=max(t
,tree_query(l
,m
,p
,q
,root
*2));
53 if q
>m
then t
:=max(t
,tree_query(m
+1,r
,p
,q
,root
*2+1));
59 procedure insedge(x
,y
:longint);
62 edge
[edge_tot
].adj
:=y
;
63 edge
[edge_tot
].next
:=ehead
[x
];
67 procedure dfs(root
:longint);
68 //solve father,dep,size,son
78 if edge
[now
].adj
<>father
[root
] then
80 father
[edge
[now
].adj
]:=root
;
81 dep
[edge
[now
].adj
]:=dep
[root
]+1;
83 inc(size
[root
],size
[edge
[now
].adj
]);
84 if size
[son
[root
]]<size
[edge
[now
].adj
] then son
[root
]:=edge
[now
].adj
;
90 procedure split(root
,tp
:longint);
99 if son
[root
]>0 then split(son
[root
],top
[root
]);
103 if (edge
[now
].adj
<>father
[root
]) and (edge
[now
].adj
<>son
[root
]) then
104 split(edge
[now
].adj
,edge
[now
].adj
);
109 procedure swap(var p
,q
:longint);
119 function find(p
,q
:longint):longint;
129 if dep
[t1
]<dep
[t2
] then
134 tmp
:=max(tmp
,tree_query(1,tot
,w
[t1
],w
[p
],1));
138 if p
=q
then exit(tmp
);
139 if dep
[p
]>dep
[q
] then swap(p
,q
);
140 exit(max(tmp
,tree_query(1,tot
,w
[son
[p
]],w
[q
],1)));
149 fillchar(ehead
,sizeof(ehead
),255);
150 fillchar(tree
,sizeof(tree
),0);
153 readln(data
[i
].x
,data
[i
].y
,data
[i
].v
);
154 insedge(data
[i
].x
,data
[i
].y
);
155 insedge(data
[i
].y
,data
[i
].x
);
164 if dep
[data
[i
].x
]>dep
[data
[i
].y
] then swap(data
[i
].x
,data
[i
].y
);
165 tree_ins(1,tot
,w
[data
[i
].y
],data
[i
].v
,1);
170 repeat read(ch
); until ch
=' ';
172 if order
='Q' then writeln(find(p
,q
)) else
173 tree_ins(1,tot
,w
[data
[p
].y
],q
,1);
176 repeat read(ch
); until (ch
=#10) or (ch
=#13);