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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| const int N = 3e5 + 9; const int M = 1e6 + 9; const int INF = 0x3f3f3f3f;
struct Graph { int cnt; int head[N], nxt[N<<1], to[N<<1], val[N<<1]; inline void add(int u, int v, int w) { nxt[++cnt] = head[u], to[cnt] = v, val[cnt] = w, head[u] = cnt; nxt[++cnt] = head[v], to[cnt] = u, val[cnt] = w, head[v] = cnt; } } G; struct Edge { int x, y, val; bool operator <(const Edge &t) const { return val < t.val; } }; struct FindSet { int fa[N]; inline void init(int n) { for (int i = 1; i <= n; ++i) fa[i] = i; } int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } } F;
int n, m, Q, tot; int a1[N], a2[N], fa[N], deep[N], son[N], size[N], top[N], id[N]; Edge ed[M]; vector <int> out[N];
void dfs1(int u, int pre) { deep[u] = deep[pre] + 1; fa[u] = pre; size[u] = 1; for (int i = G.head[u], v; i; i = G.nxt[i]) { v = G.to[i]; if (v == pre) continue; dfs1(v, u); size[u] += size[v]; if (!son[u] || size[v] > size[son[u]]) son[u] = v; } }
struct SegTree { #define ls(x) ((x) << 1) #define rs(x) ((x) << 1 | 1) int ma[N<<2]; SegTree() { for (int i = 0; i < (N<<2); ++i) ma[i] = -INF; } inline void push_up(int suc) { ma[suc] = max(ma[ls(suc)], ma[rs(suc)]); } void build(int suc, int l, int r, int *a) { if (l == r) { ma[suc] = a[l]; return; } int mid = (l + r) >> 1; build(ls(suc), l, mid, a), build(rs(suc), mid+1, r, a); push_up(suc); } void update(int suc, int l, int r, int p, int k) { if (l == r) { ma[suc] = k; return; } int mid = (l + r) >> 1; if (p <= mid) update(ls(suc), l, mid, p, k); else update(rs(suc), mid+1, r, p, k); push_up(suc); } int query(int suc, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return ma[suc]; int mid = (l + r) >> 1, res = -INF; if (ql <= mid) res = max(res, query(ls(suc), l, mid, ql, qr)); if (qr > mid) res = max(res, query(rs(suc), mid+1, r, ql, qr)); return res; } } tr1, tr2;
void dfs2(int u, int tp, int eval) { id[u] = ++tot; top[u] = tp; a1[id[u]] = eval; if (!son[u]) return; for (int i = G.head[u], v; i; i = G.nxt[i]) { v = G.to[i]; if (v == son[u]) { dfs2(v, tp, G.val[i]); break; } } for (int i = G.head[u], v; i; i = G.nxt[i]) { v = G.to[i]; if (v == fa[u] || v == son[u]) continue; dfs2(v, v, G.val[i]); } }
inline int query1(int x, int y) { int res = -INF; while (top[x] != top[y]) { if (deep[top[x]] < deep[top[y]]) swap(x, y); res = max(res, tr1.query(1, 1, n, id[top[x]], id[x])); x = fa[top[x]]; } if (deep[x] > deep[y]) swap(x, y); if (x != y) res = max(res, tr1.query(1, 1, n, id[x]+1, id[y])); return res; }
inline int query2(int x, int y) { int res = -INF; int tx = x, ty = y; tr2.update(1, 1, n, id[tx], -INF), tr2.update(1, 1, n, id[ty], -INF); while (top[x] != top[y]) { if (deep[top[x]] < deep[top[y]]) swap(x, y); res = max(res, tr2.query(1, 1, n, id[top[x]], id[x])); x = fa[top[x]]; } if (deep[x] > deep[y]) swap(x, y);
res = max(res, tr2.query(1, 1, n, id[x], id[y])); tr2.update(1, 1, n, id[tx], a2[id[tx]]), tr2.update(1, 1, n, id[ty], a2[id[ty]]); return -res; }
inline void main() { read(n), read(m), read(Q); for (int i = 1; i <= m; ++i) { read(ed[i].x), read(ed[i].y), read(ed[i].val); } F.init(n); sort(ed+1, ed+m+1); for (int i = 1; i <= m; ++i) { int x = F.find(ed[i].x), y = F.find(ed[i].y); if (x == y) continue; F.fa[x] = y; G.add(ed[i].x, ed[i].y, ed[i].val); } dfs1(1, 0), dfs2(1, 1, -INF), tr1.build(1, 1, n, a1); for (int i = 1; i <= m; ++i) { out[ed[i].x].push_back(ed[i].val); out[ed[i].y].push_back(ed[i].val); } for (int i = 1; i <= n; ++i) { sort(out[i].begin(), out[i].end()); if (out[i].size() >= 3) a2[id[i]] = -out[i][2]; else a2[id[i]] = -INF; } tr2.build(1, 1, n, a2); int x, y; while (Q--) { read(x), read(y); int q1 = query1(x, y); int q2 = query2(x, y); if (q2 == INF && out[x].size() == 1 && out[y].size() == 1) { printf("-1\\n"); continue; } if (out[x].size() >= 2) q2 = min(q2, out[x][1]); if (out[y].size() >= 2) q2 = min(q2, out[y][1]); printf("%d\\n", max(q1, q2)); } }
|