LightOJ 1101 - A Secret Mission
题意:给一个n个点,m条边的无向连通图,每条边有一个权值,然后有q个询问,问从a到b的最小代价,一条路径的代价定义为这条路径上的最大的边权值。
题解:这个题一开始想了很久dijkstra,以为是某种应用或者可以快速更新,然而并没有什么结果。
后来就想暴力吧,想优化,于是先按边权值排序,然后加边,如果边两点已经连通了,那么就肯定没必要加这条边了,然后发现这不是kruskal吗?然后就变成一棵树了,然后就变成查询树上两点最大值了,然后就变成树链剖分模板题了...然后就AC了...代码几乎和上篇一样...
#include<bits/stdc++.h> using namespace std; const int N = 50005; struct eg{ int v, nex, w; eg(){} eg(int a, int b, int c){ v = a, w = b, nex = c; } }edg[N<<2]; int fir[N], ecnt; void add(int a, int b, int c){ edg[ecnt] = eg(b, c, fir[a]); fir[a] = ecnt++; edg[ecnt] = eg(a, c, fir[b]); fir[b] = ecnt++; } int fa[N], dep[N], siz[N], son[N], root; int tree[N<<2]; void dfs(int rt){ siz[rt] = 1, son[rt] = 0; for(int k = fir[rt]; k != -1; k = edg[k].nex){ if(edg[k].v != fa[rt]){ fa[edg[k].v] = rt; dep[edg[k].v] = dep[rt] + 1; dfs(edg[k].v); if(siz[edg[k].v] > siz[son[rt]]) son[rt] = edg[k].v; siz[rt] += siz[edg[k].v]; } } } int w[N<<2], top[N], cnt; //w[]为到线段树的映射 void dfs2(int rt, int tp){ w[rt] = ++cnt; top[rt] = tp; if(son[rt]) dfs2(son[rt], top[rt]); for(int k = fir[rt]; k != -1; k = edg[k].nex){ if(edg[k].v != son[rt] && edg[k].v != fa[rt]){ dfs2(edg[k].v, edg[k].v); } } } void update(int rt, int l, int r, int pos, int val){ if(l == r){ tree[rt] = val; return; } int mid = (l+r) >> 1; if(pos <= mid) update(rt<<1, l, mid, pos, val); else update(rt<<1|1, mid+1, r, pos, val); tree[rt] = max(tree[rt<<1], tree[rt<<1|1]); } int n, m; struct E{ int u, v, w; bool operator < (E a) const{ return w < a.w; } }sav[N<<1]; int seed[N]; int find(int x){ return seed[x] < 0? x : seed[x] = find(seed[x]); } int join(int a, int b){ a = find(a), b = find(b); if(a == b) return 0; if(seed[a] > seed[b]) seed[a] = b; else seed[b] = a; return 1; } bool in[N<<1]; void init(){ scanf("%d %d", &n, &m); memset(fir, -1, sizeof(fir)); memset(siz, 0, sizeof(siz)); memset(tree, 0, sizeof(tree)); memset(seed, -1, sizeof(seed)); root = (n+1)/2; fa[root] = cnt = ecnt = dep[root] = 0; int a, b, c; for(int i = 0; i < m; ++i) scanf("%d%d%d", &sav[i].u, &sav[i].v, &sav[i].w), in[i] = 0; sort(sav, sav+m); for(int i = 0; i < m; ++i){ if(join(sav[i].u, sav[i].v)) add(sav[i].u, sav[i].v, sav[i].w), in[i] = 1; } dfs(root); dfs2(root, root); for(int i = 0; i < m; ++i){ if(!in[i]) continue; if(dep[sav[i].u] > dep[sav[i].v]) swap(sav[i].u, sav[i].v); update(1, 1, cnt, w[sav[i].v], sav[i].w); } } int maxi(int rt, int l, int r, int ql, int qr){ if( ql > r || qr < l) return 0; if(ql <= l && qr >= r) return tree[rt]; int mid = (l+r) >> 1; return max(maxi(rt<<1, l, mid, ql, qr), maxi(rt<<1|1, mid+1, r, ql, qr)); } int solve(int va, int vb){ int f1 = top[va], f2 = top[vb], tmp = 0; while(f1 != f2){ if(dep[f1] < dep[f2]){ swap(f1, f2); swap(va, vb); } tmp = max(tmp, maxi(1, 1, cnt, w[f1], w[va])); va = fa[f1], f1 = top[va]; } if(va == vb) return tmp; if(dep[va] > dep[vb]) swap(va, vb); return max(tmp, maxi(1, 1, cnt, w[son[va]], w[vb])); } int main(){ int T, ca = 1; scanf("%d", &T); while(T--){ init(); int q; scanf("%d", &q); printf("Case %d:\n", ca++); while(q--){ int a, b; scanf("%d%d", &a, &b); printf("%d\n", solve(a, b)); } } }