#define __USE_MINGW_ANSI_STDIO 0
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <stack>
#include <deque>
#include <string.h>
#include <sstream>
#include <math.h>
using namespace std;
#define PI atan2(0, -1)
#define epsilon 0.000000001
#define INF 5000000000000000000
#define MOD 1000000007
template<int sz>
class HeavyLight{
public:
struct Node{
int mini, lazy;
Node(){ mini = 2e9; lazy = 0; }
};
int N, M, parents [sz+10], depths [sz+10], subtreeSizes [sz+10];
int heavy [sz+10], root [sz+10], segPos [sz+10];
Node tree [4*sz+10];
pair<int, int> originals [sz+10];
vector<pair<int, int>> adjacency [sz+10];
HeavyLight(){ memset(heavy, -1, sizeof(heavy)); }
void dfs(int curr, int prevy, int d){
depths[curr] = d; parents[curr] = prevy; subtreeSizes[curr] = 1;
int maxSubtree = 0;
for(pair<int, int> edge : adjacency[curr]){
if(edge.first == prevy) continue;
dfs(edge.first, curr, d+1);
if(subtreeSizes[edge.first] > maxSubtree) heavy[curr] = edge.first, maxSubtree = subtreeSizes[edge.first];
subtreeSizes[curr] += subtreeSizes[edge.first];
}
}
int left(int p){ return p<<1; }
int right(int p){ return (p<<1)+1; }
void pushDown(int p, int L, int R){
if(tree[p].lazy == 0) return;
tree[p].mini = min(tree[p].mini, tree[p].lazy);
int li = left(p), ri = right(p);
if(L != R){
tree[li].lazy = tree[p].lazy;
tree[ri].lazy = tree[p].lazy;
}
tree[p].lazy = 0;
}
long long evalMini(int p){ return min(tree[p].mini, tree[p].lazy); }
void pullUp(int p, int L, int R){
int li = left(p), ri = right(p);
tree[p].mini = min(evalMini(li), evalMini(ri));
}
void update(int p, int L, int R, int i, int j, int val){
if(L > R || i > R || j < L) return;
if(L >= i && R <= j){ tree[p].lazy = val; return; }
pushDown(p, L, R);
int li = left(p), ri = right(p);
update(li, L, (L+R)/2, i, j, val); update(ri, (L+R)/2+1, R, i, j, val);
pullUp(p, L, R);
}
int minQuery(int p, int L, int R, int i, int j){
if(L > R || i > R || j < L) return 2e9;
if(L >= i && R <= j) return evalMini(p);
pushDown(p, L, R);
int li = left(p), ri = right(p);
int ret = min(minQuery(li, L, (L+R)/2, i, j), minQuery(ri, (L+R)/2+1, R, i, j));
pullUp(p, L, R);
return ret;
}
void HLD(){
for(int i = 0, currentPos = 0; i < N; i++)
if(parents[i] == -1 || heavy[parents[i]] != i)
for(int j = i; j != -1; j = heavy[j]){
root[j] = i;
segPos[j] = currentPos++;
}
}
template<class BinaryOperation>
void processPath(int u, int v, BinaryOperation op){
for( ; root[u] != root[v]; v = parents[root[v]]){
if(depths[root[u]] > depths[root[v]]) swap(u, v);
op(segPos[root[v]], segPos[v]);
}
if(depths[u] > depths[v]) swap(u, v);
op(segPos[u]+1, segPos[v]);
}
void modifyPath(int u, int v, int value){
processPath(u, v, [this, &value](int l, int r) { update(1, 0, N-1, l, r, value); });
}
int queryPath(int u, int v) {
int ret = 2e9;
processPath(u, v, [this, &ret](int l, int r) { ret = min(ret, minQuery(1, 0, N-1, l, r)); });
return ret;
}
};
int main(){
//freopen("disrupt.in", "r", stdin); freopen("disrupt.out", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(18);
HeavyLight<50000> hld;
cin >> hld.N >> hld.M;
for(int i = 0; i < hld.N-1; i++){
int a, b; cin >> a >> b; a--; b--;
hld.originals[i] = make_pair(a, b);
hld.adjacency[a].push_back(make_pair(b, 2e9)); hld.adjacency[b].push_back(make_pair(a, 2e9));
}
hld.dfs(0, -1, 0); hld.HLD();
vector<pair<int, pair<int, int>>> queries;
for(int i = 0; i < hld.M; i++){
int a, b, c; cin >> a >> b >> c; a--; b--;
queries.push_back(make_pair(c, make_pair(a, b)));
}
sort(queries.begin(), queries.end());
for(int i = hld.M-1; i > -1; i--){
int a = queries[i].second.first, b = queries[i].second.second, c = queries[i].first;
hld.modifyPath(a, b, c);
}
for(int i = 0; i < hld.N-1; i++){
int ret = hld.queryPath(hld.originals[i].first, hld.originals[i].second);
if(ret == 2e9) cout << "-1\n";
else cout << ret << '\n';
}
return 0;
}
I2RlZmluZSBfX1VTRV9NSU5HV19BTlNJX1NURElPIDAKI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW9tYW5pcD4KI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8dW5vcmRlcmVkX21hcD4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPHVub3JkZXJlZF9zZXQ+CiNpbmNsdWRlIDxzdGFjaz4KI2luY2x1ZGUgPGRlcXVlPgojaW5jbHVkZSA8c3RyaW5nLmg+CiNpbmNsdWRlIDxzc3RyZWFtPgojaW5jbHVkZSA8bWF0aC5oPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgUEkgYXRhbjIoMCwgLTEpCiNkZWZpbmUgZXBzaWxvbiAwLjAwMDAwMDAwMQojZGVmaW5lIElORiA1MDAwMDAwMDAwMDAwMDAwMDAwCiNkZWZpbmUgTU9EIDEwMDAwMDAwMDcKCnRlbXBsYXRlPGludCBzej4KY2xhc3MgSGVhdnlMaWdodHsKcHVibGljOgogICAgc3RydWN0IE5vZGV7CiAgICAgICAgaW50IG1pbmksIGxhenk7CiAgICAgICAgTm9kZSgpeyBtaW5pID0gMmU5OyBsYXp5ID0gMDsgfQogICAgfTsKCiAgICBpbnQgTiwgTSwgcGFyZW50cyBbc3orMTBdLCBkZXB0aHMgW3N6KzEwXSwgc3VidHJlZVNpemVzIFtzeisxMF07CiAgICBpbnQgaGVhdnkgW3N6KzEwXSwgcm9vdCBbc3orMTBdLCBzZWdQb3MgW3N6KzEwXTsKICAgIE5vZGUgdHJlZSBbN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YXp5KTsKICAgICAgICBpbnQgbGkgPSBsZWZ0KHApLCByaSA9IHJpZ2h0KHApOwogICAgICAgIGlmKEwgIT0gUil7CiAgICAgICAgICAgIHRyZWVbbGldLmxhenkgPSB0cmVlW3BdLmxhenk7CiAgICAgICAgICAgIHRyZWVbcmldLmxhenkgPSB0cmVlW3BdLmxhenk7CiAgICAgICAgfQogICAgICAgIHRyZWVbcF0ubGF6eSA9IDA7CiAgICB9CgogICAgbG9uZyBsb25nIGV2YWxNaW5pKGludCBwKXsgcmV0dXJuIG1pbih0cmVlW3BdLm1pbmksIHRyZWVbcF0ubGF6eSk7IH0KCiAgICB2b2lkIHB1bGxVcChpbnQgcCwgaW50IEwsIGludCBSKXsKICAgICAgICBpbnQgbGkgPSBsZWZ0KHApLCByaSA9IHJpZ2h0KHApOwogICAgICAgIHRyZWVbcF0ubWluaSA9IG1pbihldmFsTWluaShsaSksIGV2YWxNaW5pKHJpKSk7CiAgICB9CgogICAgdm9pZCB1cGRhdGUoaW50IHAsIGludCBMLCBpbnQgUiwgaW50IGksIGludCBqLCBpbnQgdmFsKXsKICAgICAgICBpZihMID4gUiB8fCBpID4gUiB8fCBqIDwgTCkgcmV0dXJuOwogICAgICAgIGlmKEwgPj0gaSAmJiBSIDw9IGopeyB0cmVlW3BdLmxhenkgPSB2YWw7IHJldHVybjsgfQogICAgICAgIHB1c2hEb3duKHAsIEwsIFIpOwogICAgICAgIGludCBsaSA9IGxlZnQocCksIHJpID0gcmlnaHQocCk7CiAgICAgICAgdXBkYXRlKGxpLCBMLCAoTCtSKS8yLCBpLCBqLCB2YWwpOyB1cGRhdGUocmksIChMK1IpLzIrMSwgUiwgaSwgaiwgdmFsKTsKICAgICAgICBwdWxsVXAocCwgTCwgUik7CiAgICB9CgogICAgaW50IG1pblF1ZXJ5KGludCBwLCBpbnQgTCwgaW50IFIsIGludCBpLCBpbnQgail7CiAgICAgICAgaWYoTCA+IFIgfHwgaSA+IFIgfHwgaiA8IEwpIHJldHVybiAyZTk7CiAgICAgICAgaWYoTCA+PSBpICYmIFIgPD0gaikgcmV0dXJuIGV2YWxNaW5pKHApOwogICAgICAgIHB1c2hEb3duKHAsIEwsIFIpOwogICAgICAgIGludCBsaSA9IGxlZnQocCksIHJpID0gcmlnaHQocCk7CiAgICAgICAgaW50IHJldCA9IG1pbihtaW5RdWVyeShsaSwgTCwgKEwrUikvMiwgaSwgaiksIG1pblF1ZXJ5KHJpLCAoTCtSKS8yKzEsIFIsIGksIGopKTsKICAgICAgICBwdWxsVXAocCwgTCwgUik7CiAgICAgICAgcmV0dXJuIHJldDsKICAgIH0KCiAgICB2b2lkIEhMRCgpewogICAgICAgIGZvcihpbnQgaSA9IDAsIGN1cnJlbnRQb3MgPSAwOyBpIDwgTjsgaSsrKQogICAgICAgICAgICBpZihwYXJlbnRzW2ldID09IC0xIHx8IGhlYXZ5W3BhcmVudHNbaV1dICE9IGkpCiAgICAgICAgICAgICAgICBmb3IoaW50IGogPSBpOyBqICE9IC0xOyBqID0gaGVhdnlbal0pewogICAgICAgICAgICAgICAgICAgIHJvb3Rbal0gPSBpOwogICAgICAgICAgICAgICAgICAgIHNlZ1Bvc1tqXSA9IGN1cnJlbnRQb3MrKzsKICAgICAgICAgICAgICAgIH0KICAgIH0KCiAgICB0ZW1wbGF0ZTxjbGFzcyBCaW5hcnlPcGVyYXRpb24+CiAgICB2b2lkIHByb2Nlc3NQYXRoKGludCB1LCBpbnQgdiwgQmluYXJ5T3BlcmF0aW9uIG9wKXsKICAgICAgICBmb3IoIDsgcm9vdFt1XSAhPSByb290W3ZdOyB2ID0gcGFyZW50c1tyb290W3ZdXSl7CiAgICAgICAgICAgIGlmKGRlcHRoc1tyb290W3VdXSA+IGRlcHRoc1tyb290W3ZdXSkgc3dhcCh1LCB2KTsKICAgICAgICAgICAgb3Aoc2VnUG9zW3Jvb3Rbdl1dLCBzZWdQb3Nbdl0pOwogICAgICAgIH0KICAgICAgICBpZihkZXB0aHNbdV0gPiBkZXB0aHNbdl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