#define debug(x) cout << #x << " is " << x << endl #define inc(i, a, b) for (int i = a; i <= b; ++i) typedeflonglong ll; constint INF = 0x3f3f3f3f, N = 1e5 + 5;
int n, m; int a[N], f[N][20], mn[N];
voidinit(){ mn[0] = -1; for (int i = 1; i <= n; ++i) { mn[i] = (i&(i-1)) == 0 ? mn[i-1] + 1 : mn[i-1]; f[i][0] = a[i]; } for (int j = 1; j <= mn[n]; ++j) for (int i = 1; i + (1<<j) - 1 <= n; ++i) f[i][j] = max(f[i][j-1], f[i+(1<<(j-1))][j-1]); }
intquery(int l, int r){ int k = mn[r-l+1]; return max(f[l][k], f[r-(1<<k)+1][k]); }
intmain() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); init(); int x, y; while (m--) { scanf("%d%d", &x, &y); printf("%d\n", query(x, y)); } return0; }
#define debug(x) cout << #x << " is " << x << endl #define inc(i, a, b) for (int i = a; i <= b; ++i) typedeflonglong ll; constint INF = 0x3f3f3f3f, N = 4e4 + 5;
int t, n, m, tot, k; int f[N][20], d[N], dis[N], head[N], lg[N]; int ver[N<<1], edge[N<<1], nxt[N<<1];
voidadd(int x, int y, int z){ ver[++tot] = y; edge[tot] = z; nxt[tot] = head[x]; head[x] = tot; }
voidbfs(){ queue<int> q; q.push(1); d[1] = 1; while (!q.empty()) { int x = q.front(); q.pop(); for (int i = head[x]; i; i = nxt[i]) { int y = ver[i]; if (d[y]) continue; d[y] = d[x] + 1; dis[y] = dis[x] + edge[i]; f[y][0] = x; for (int j = 1; j <= k; ++j) f[y][j] = f[f[y][j-1]][j-1]; q.push(y); } } }
intlca(int x, int y){ if (d[x] > d[y]) swap(x, y); for (int i = k; i >= 0; --i) if (d[f[y][i]] >= d[x]) y = f[y][i]; if (x == y) return x; for (int i = k; i >= 0; --i) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i]; return f[x][0]; }
intmain() { scanf("%d", &t); for (int i = 2; i < N; ++i) lg[i] = lg[i/2] + 1; while (t--) { scanf("%d%d", &n, &m); k = lg[n] + 1; for (int i = 1; i <= n; ++i) head[i] = d[i] = 0; tot = 0; int x, y, z; for (int i = 1; i < n; ++i) { scanf("%d%d%d", &x, &y, &z); add(x, y, z); add(y, x, z); } bfs(); for (int i = 1; i <= m; ++i) { scanf("%d%d", &x, &y); printf("%d\n", dis[x] + dis[y] - 2 * dis[lca(x, y)]); } } return0; }
#define debug(x) cout << #x << " is " << x << endl #define inc(i, a, b) for (int i = a; i <= b; ++i) typedeflonglong ll; constint INF = 0x3f3f3f3f, N = 4e4 + 5, M = 205;
int t, n, m, tot; int fa[N], d[N], v[N], ans[M]; int ver[N<<1], edge[N<<1], nxt[N<<1], head[N];
vector<int> q[N], qid[N];
voidadd(int x, int y, int z){ ver[++tot] = y; edge[tot] = z; nxt[tot] = head[x]; head[x] = tot; }
voidaddquery(int x, int y, int id){ q[x].push_back(y), qid[x].push_back(id); q[y].push_back(x), qid[y].push_back(id); }
voidtarjan(int x){ v[x] = 1; for (int i = head[x]; i; i = nxt[i]) { int y = ver[i]; if (v[y]) continue; d[y] = d[x] + edge[i]; tarjan(y); fa[y] = x; } for (int i = 0; i < (int)q[x].size(); ++i) { int y = q[x][i], id = qid[x][i]; if (v[y] == 2) { int lca = find(y); ans[id] = min(ans[id], d[x] + d[y] - 2 * d[lca]); } } v[x] = 2; }
intmain() { scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { head[i] = 0; fa[i] = i; v[i] = 0; q[i].clear(); qid[i].clear(); } tot = 0; int x, y, z; for (int i = 1; i < n; ++i) { scanf("%d%d%d", &x, &y, &z); add(x, y, z); add(y, x, z); } for (int i = 1; i <= m; ++i) { scanf("%d%d", &x, &y); if (x == y) { ans[i] = 0; continue; } addquery(x, y, i); ans[i] = INF; } tarjan(1); for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); } return0; }