1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| #include <bits/stdc++.h> using namespace std;
const int MAXN = 1000010;
struct Node { int id, dis, from; };
struct Edge { int nxt; int to; int w; bool vis; } edge[MAXN * 2]; priority_queue<Node> q; int head[MAXN], cnt; int N, M, Q; int minn[MAXN][21]; int fa[MAXN][21], dep[MAXN];
namespace Prim { int vis[MAXN]; }
namespace LCA { int col[MAXN]; int cnt; }
bool operator<(const Node& a, const Node& b) { return a.dis < b.dis; }
void link(int u, int v, int w) { edge[++cnt].nxt = head[u]; edge[cnt].to = v; edge[cnt].w = w; head[u] = cnt; }
void prim() { for (int i = 1; i <= N; i++) { if (Prim::vis[i]) continue; q.push(Node{i, 0, 0}); while (!q.empty()) { Node u = q.top(); q.pop(); if (Prim::vis[u.id]) continue; Prim::vis[u.id] = 1;
edge[u.from].vis = 1; if (u.from & 1) { edge[u.from + 1].vis = 1; } else { edge[u.from - 1].vis = 1; }
for (int i = head[u.id]; i; i = edge[i].nxt) { int to = edge[i].to; int w = edge[i].w; int from = i; if (Prim::vis[to]) continue; q.push(Node{to, w, from}); } } } }
void dfs(int x, int father, int w, int col) { if (LCA::col[x]) return; LCA::col[x] = col; fa[x][0] = father; minn[x][0] = w; dep[x] = dep[father] + 1; for (int i = 1; (1 << i) <= dep[x]; i++) { fa[x][i] = fa[fa[x][i - 1]][i - 1]; minn[x][i] = min(minn[x][i - 1], minn[fa[x][i - 1]][i - 1]); } for (int i = head[x]; i; i = edge[i].nxt) { int to = edge[i].to; int cw = edge[i].w; if (to == father) continue; if (!edge[i].vis) continue; dfs(to, x, cw, col); } }
int getlca(int a, int b) { if (LCA::col[a] != LCA::col[b]) return -1; int ans = 0x7fffffff; if (dep[a] < dep[b]) swap(a, b); for (int i = 20; i >= 0; i--) { if (dep[a] - (1 << i) >= dep[b]) { ans = min(ans, minn[a][i]); a = fa[a][i]; } } if (a == b) return ans; for (int i = 20; i >= 0; i--) { if (fa[a][i] == fa[b][i]) continue;
ans = min(ans, min(minn[a][i], minn[b][i])); a = fa[a][i], b = fa[b][i]; } return min(ans, min(minn[a][0], minn[b][0])); }
int main() { cin >> N >> M; for (int i = 1; i <= M; i++) { int u, v, w; cin >> u >> v >> w; link(u, v, w); link(v, u, w); } prim();
cin >> Q; memset(minn, 0x3f, sizeof(minn)); for (int i = 1; i <= N; i++) { dfs(i, 0, 0, ++LCA::cnt); } for (int i = 1; i <= Q; i++) { int a, b; cin >> a >> b; cout << getlca(a, b) << endl; }
return 0; }
|