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
| #include <iostream> using namespace std; int parent[10005]; int Size[10005]; struct edge { int start; int end; int weight; }; void merge(const edge *aBegin, const edge *aEnd, const edge *bBegin, const edge *bEnd, edge *c) { while (aBegin != aEnd && bBegin != bEnd) { if (bBegin->weight > aBegin->weight) { *c = *bBegin; ++bBegin; } else { *c = *aBegin; ++aBegin; } ++c; } for (; aBegin != aEnd; ++aBegin, ++c) *c = *aBegin; for (; bBegin != bEnd; ++bBegin, ++c) *c = *bBegin; } void merge_sort(edge *a, int l, int r) { if (r - l <= 1) return; int mid = l + ((r - l) >> 1); merge_sort(a, l, mid), merge_sort(a, mid, r); edge* tmp = new edge[r - l + 1]; merge(a + l, a + mid, a + mid, a + r, tmp); for (int i = l; i < r; ++i) a[i] = tmp[i - l]; delete []tmp; } int find(int u) { if(parent[u] != u) { parent[u] = find(parent[u]); } return parent[u]; } void unite(int x,int y) { int rootX = find(x); int rootY = find(y); if(rootX != rootY) { if(Size[rootX] > Size[rootY]) { parent[rootY] = rootX; Size[rootX] += Size[rootY]; }else { parent[rootX] = rootY; Size[rootY] += Size[rootX]; } } } int n,m,q; int u,v,w; edge Graph[50005]; edge MST[10005]; struct adjEdge { adjEdge *next; int end; int weight; }; struct adjVer { adjEdge *head; }; adjVer adjMST[10005];//最大生成树邻接表 void add_edge(int u,int v,int w) { adjEdge *current = new adjEdge; current->end = v; current->weight = w; current->next = adjMST[u].head; adjMST[u].head = current; } int fa[10005][21]; int depth[10005]; void dfs(int sn,int f,int h) { fa[sn][0] = f; depth[sn] = h; adjEdge *current = adjMST[sn].head; while(current != nullptr) { if(current->end != f) { dfs(current->end,sn,h + 1); } current = current->next; } } int lca(int u,int v) { if(depth[u] < depth[v]) { swap(u,v); } for(int j = 20;j >= 0;j if(depth[fa[u][j]] >= depth[v]) { u = fa[u][j]; } } if(u == v) { return u; } for(int j = 20;j >= 0;j if(fa[u][j] != fa[v][j]) { u = fa[u][j]; v = fa[v][j]; } } return fa[u][0]; } int findVal(int u,int v) { adjEdge *current = adjMST[u].head; while(current != nullptr) { if(current->end == v) { return current->weight; } current = current->next; } return 0; } int getMin(int u,int lca) { int Min = 1e9; while(u != lca) { int v = findVal(u,fa[u][0]); if(v < Min) { Min = v; } u = fa[u][0]; } return Min; } int main() { cin >> n >> m; for(int i = 1;i <= n;i ++) { parent[i] = i; Size[i] = 1; } for(int i = 1;i <= m;i ++) { cin >> u >> v >> w; Graph[i].start = u; Graph[i].end = v; Graph[i].weight = w; } merge_sort(Graph,1,m + 1); int cnt = 0; for(int i = 1;i <= m;i ++) { int rst = find(Graph[i].start); int rnd = find(Graph[i].end); if(rst != rnd) { unite(rst,rnd); MST[++cnt] = Graph[i]; } } for(int i = 1;i <= cnt;i ++) { add_edge(MST[i].start,MST[i].end,MST[i].weight); add_edge(MST[i].end,MST[i].start,MST[i].weight); } dfs(1,0,1); for(int j = 1;j <= 20;j ++) { for(int i = 1;i <= n;i ++) { fa[i][j] = fa[fa[i][j - 1]][j - 1]; } } cin >> q; int start,end; for(int i = 1;i <= q;i ++) { cin >> start >> end; if(find(start) != find(end)) { cout << -1 << '\n'; }else { int LCA = lca(start,end); cout << min(getMin(start,LCA),getMin(end,LCA)) << '\n'; } } return 0; }
|