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

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. [HTML & CSS] HTML和CSS基础知识

    最近将博客简单地修饰了下,需要用到HTML和CSS代码,花了一天时间学习了一下这两方面的知识.虽然内容很简单,但是足够用来修改自己的博客了. 1. HTML 1.1. HTML介绍 HTML与CSS的 ...

  2. Python类的特殊属性

    Python中的特殊属性 定义如下类: class Foo(object): """Foo class definition""" 类的特殊 ...

  3. codeforces 715c

    题目大意:给定一个有N个点的树,问其中有多少条路径满足他们的边权连成的数对M取余为0.其中gcd(M,10)=1. 题解: 很亲民的点分治题目,对每一层点分治,预处理每个点到当前根的数字并对m取余,和 ...

  4. 脚踏实地学C#1-基元类型

    基元类型:编译器直接支持的数据类型 基元类型直接映射到FCL类库上,如int 和Int32是等价的,只不过是int是c#提供的,Int32是FCL类库提供的. int只是Int32的别名 using ...

  5. BZOJ_4809_皇后_爆搜

    BZOJ_4809_皇后_爆搜 Description 众所不知,rly现在不会玩国际象棋.但是,作为一个OIer,rly当然做过八皇后问题.这里再啰嗦几句,皇后可以攻击到同行同列同对角线,在n*n的 ...

  6. bzoj 2600 ricehub

    2600: [Ioi2011]ricehub Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 783  Solved: 417[Submit][Stat ...

  7. window.open全屏

    window.open全屏   1. window.open(url,'资金计划项超支提醒','width='+(window.screen.availWidth-10)+',height='+(wi ...

  8. 用HTML 5打造斯诺克桌球俱乐部(1) – 51CTO.COM

    本文介绍了如何利用HTML5技术来打造一款非常酷的斯诺克桌球游戏,文章中详细地列… 查阅全文 ›

  9. 【eclipse插件开发实战】Eclipse插件开发3——OSGi、RCP

    Eclipse插件开发实战3--OSGi.RCP 一.OSGi 1. 什么是OSGi框架 OSGi(Open Service Gateway Initiative)框架是运行在JavaVM环境里的服务 ...

  10. PHP文件操作的经典案例

    <?php /* 遍历目录函数,只读取目录的最外层的内容 */ function readDirectory($path){ $handle = opendir($path); while(($ ...