最小生成树上倍增询问裸的。

const int maxn = 2e5 + 5;
int n, m, q;
//图
struct Edge {
int u, v;
ll cost; bool operator < (const Edge &rhs) const {
return cost < rhs.cost;
}
}e[maxn];
map<P, ll> mp;
//最小生成树
int fa[maxn];
vector<int> vc[maxn];
ll mst;
//树上倍增
int f[maxn][20], T, d[maxn];
ll dis[maxn][20]; int getf(int v) {
return v == fa[v] ? v : fa[v] = getf(fa[v]);
} void kruscal() {
rep(i, 1, n) fa[i] = i;
sort(e + 1, e + 1 + m); rep(i, 1, m) {
int t = getf(e[i].u), p = getf(e[i].v);
if (t != p) {
int u = e[i].u, v = e[i].v;
vc[u].push_back(v);
vc[v].push_back(u);
mst += e[i].cost;
fa[t] = p;
}
}
} void bfs() {
T = (int)(log(n) / log(2)) + 1;
queue<int> Q;
Q.push(1), d[1] = 1; while (!Q.empty()) {
int x = Q.front(); Q.pop();
for (auto y : vc[x]) {
if (d[y]) continue; Q.push(y);
f[y][0] = x;
d[y] = d[x] + 1;
dis[y][0] = mp[P(y, x)]; rep(i, 1, T) {
f[y][i] = f[f[y][i - 1]][i - 1];
dis[y][i] = max(dis[f[y][i - 1]][i - 1], dis[y][i - 1]);
}
}
}
} ll lca(int x, int y) {
ll ret = 0;
if (d[x] > d[y]) swap(x, y); irep(i, T, 0)
if (d[f[y][i]] >= d[x]) {
ret = max(ret, dis[y][i]);
y = f[y][i];
} if (x == y) return ret; irep(i, T, 0)
if (f[y][i] != f[x][i]) {
ret = max(ret, max(dis[y][i], dis[x][i]));
y = f[y][i], x = f[x][i];
} return max(ret, max(dis[y][0], dis[x][0]));
} int main() {
read(n), read(m);
rep(i, 1, m) {
read(e[i].u);
read(e[i].v);
read(e[i].cost);
mp[P(e[i].u, e[i].v)] = mp[P(e[i].v, e[i].u)] = e[i].cost;
} kruscal();
bfs(); for (read(q); q; q--) {
int u, v;
read(u), read(v);
writeln(mst - lca(u, v) + mp[P(u, v)]);
}
return 0;
}

GYM 101889I(mst+lca)的更多相关文章

  1. UVA 11354 Bond(MST + LCA)

    n<=50000, m<=100000的无向图,对于Q<=50000个询问,每次求q->p的瓶颈路. 其实求瓶颈路数组maxcost[u][v]有用邻接矩阵prim的方法.但是 ...

  2. Gym Class(拓扑排序)

    Gym Class Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  3. 【BZOJ 2144】 2144: 跳跳棋 (倍增LCA)

    2144: 跳跳棋 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 642  Solved: 307 Description 跳跳棋是在一条数轴上进行的 ...

  4. Minimum spanning tree for each edge(倍增LCA)

    https://vjudge.net/contest/320992#problem/J 暑期训练的题. 题意:给你一个n个点,m条边的无向图.对于每一条边,求包括该边的最小生成树. 思路:首先想到求一 ...

  5. C - Line-line Intersection Gym - 102220C(线段相交)

    There are n lines l1,l2,…,ln on the 2D-plane. Staring at these lines, Calabash is wondering how many ...

  6. POJ3694 Network(连通图+LCA)

    题目链接:http://poj.org/problem?id=3694 题目大意:给定一个图,每次添加一条边(可能有重边).输出每次添加后桥的 数目.由于添加边的次数比较多,添加一次Tarjin一次明 ...

  7. lightoj-1128-Greatest Parent(二分+LCA)

    传送门 首先我要实力吐槽这个lightoj 它给我的注册密码藏在不为人所见的地方 注册注册了10多分钟 qwq -------------------------------------------- ...

  8. HDU 2460 Network(桥+LCA)

    http://acm.hdu.edu.cn/showproblem.php?pid=2460 题意:给出图,求每次增加一条边后图中桥的数量. 思路: 先用tarjan算法找出图中所有的桥,如果lowv ...

  9. bzoj1602 / P2912 [USACO08OCT]牧场散步Pasture Walking(倍增lca)

    P2912 [USACO08OCT]牧场散步Pasture Walking 求树上两点间路径--->lca 使用倍增处理lca(树剖多长鸭) #include<iostream> # ...

随机推荐

  1. Windows窗口程序从创建到关闭产生的消息

    Windows是消息驱动的,理解消息机制及消息循环是特别重要.知道在什么情况下产生什么消息会让我们对程序有更好的控制.Windows给应用程序发消息,有些会加入应用程序的消息队列,也是就是队列消息.有 ...

  2. [zjoi]青蛙的约会_扩展欧几里德

    两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝着对方那里跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚 ...

  3. "未预编译文件 因此不能请求该文件"问题处理

    手里一个项目重新编译后发布,访问时提示未预编译文件“default.aspx”, 因此不能请求该文件.综合网上的解决方法,做了如下操作: 1.重新安装了AJAX Extension: 2.项目添加引用 ...

  4. ModuleNotFoundError: No module named 'numpy.core._multiarray_umath' ImportError: numpy.core.multiarray failed to import

      出现以下错误:可能是因为你的numpy版本太低 更新numpy的版本 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgra ...

  5. 让tomcat启动时,自动加载你的项目

    在tomcat-->conf-->serve.xml文件最后加上 <Context path="/atest" docBase="E:\Workspac ...

  6. elasticsearch function_score Query——文档排序结果的最后一道墙

    function_score Query The function_score query is the ultimate tool for taking control of the scoring ...

  7. CSS实现文字内容不被截断当超出指定长度时该字符串自动整体换到下一行

    效果图: 1.内容不被截断 span {       overflow:hidden;       white-space:nowrap;       text-overflow:ellipsis;  ...

  8. 51nod-1065:最小正子段和(STL)

    N个整数组成的序列a11,a22,a33,…,ann,从中选出一个子序列(aii,ai+1i+1,…ajj),使这个子序列的和>0,并且这个和是所有和>0的子序列中最小的. 例如:4,-1 ...

  9. BZOJ_3729_Gty的游戏_博弈论+splay+dfs序

    BZOJ_3729_Gty的游戏_博弈论+splay+dfs序 Description 某一天gty在与他的妹子玩游戏. 妹子提出一个游戏,给定一棵有根树,每个节点有一些石子,每次可以将不多于L的石子 ...

  10. js的浅克隆和深克隆

    谈一谈个人对js浅克隆和深克隆的区别. 之前也看到很多博客在写,当然也有写的非常好的,但是个人觉得既然要分享就不要写的太深奥,尽量以简单易懂为主. 浅克隆其实就是 对象A = 对象B:如果改变了对象B ...