GYM 101889I(mst+lca)
最小生成树上倍增询问裸的。
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)的更多相关文章
- UVA 11354 Bond(MST + LCA)
n<=50000, m<=100000的无向图,对于Q<=50000个询问,每次求q->p的瓶颈路. 其实求瓶颈路数组maxcost[u][v]有用邻接矩阵prim的方法.但是 ...
- Gym Class(拓扑排序)
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- 【BZOJ 2144】 2144: 跳跳棋 (倍增LCA)
2144: 跳跳棋 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 642 Solved: 307 Description 跳跳棋是在一条数轴上进行的 ...
- Minimum spanning tree for each edge(倍增LCA)
https://vjudge.net/contest/320992#problem/J 暑期训练的题. 题意:给你一个n个点,m条边的无向图.对于每一条边,求包括该边的最小生成树. 思路:首先想到求一 ...
- 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 ...
- POJ3694 Network(连通图+LCA)
题目链接:http://poj.org/problem?id=3694 题目大意:给定一个图,每次添加一条边(可能有重边).输出每次添加后桥的 数目.由于添加边的次数比较多,添加一次Tarjin一次明 ...
- lightoj-1128-Greatest Parent(二分+LCA)
传送门 首先我要实力吐槽这个lightoj 它给我的注册密码藏在不为人所见的地方 注册注册了10多分钟 qwq -------------------------------------------- ...
- HDU 2460 Network(桥+LCA)
http://acm.hdu.edu.cn/showproblem.php?pid=2460 题意:给出图,求每次增加一条边后图中桥的数量. 思路: 先用tarjan算法找出图中所有的桥,如果lowv ...
- bzoj1602 / P2912 [USACO08OCT]牧场散步Pasture Walking(倍增lca)
P2912 [USACO08OCT]牧场散步Pasture Walking 求树上两点间路径--->lca 使用倍增处理lca(树剖多长鸭) #include<iostream> # ...
随机推荐
- ubuntu 下编译安装ceph
git clone --recursive https://github.com/ceph/ceph.git cd ceph/ sudo apt-get install libtool sud ...
- Unable to resolve target 'android-16'
Just now when I imported the "android-support-v7-appcompat" to ADT,the console pointed out ...
- 关于SelectObject之后是否要恢复之前的GDI对象
以下列代码为例 { // 创建内存DC CDC mMemDc; mMemDc.CreateCompatibleDC( &dc ); // 创建兼容位图 CBitmap bmpMemBmp; b ...
- (转)vim 访问系统剪贴板
原文出处:http://vim.wikia.com/wiki/Accessing_the_system_clipboard Please review this tip: This tip was i ...
- python 基础之第四天
例子1: 打印列表每个元素对应的索引 [root@master script]# vim suoyin.py #!/usr/bin/python # coding:utf-8 alist = ['fu ...
- bzoj 5072 [Lydsy1710月赛]小A的树——树形dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5072 发现对于每个子树,黑点个数确定时,连通块的大小取值范围一定是一段区间:所以考虑只最小化 ...
- HDU 1394 树状数组+离散化求逆序数
对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们 ...
- 微信小程序基础组件
所有的组件与属性都是小写,以连字符 - 连接 共同的属性类型: class id style bind*/catch* hidden data-* block标签. <block> 并不是 ...
- Ubuntu12.04下安装、使用、卸载MySQL
转自:http://blog.csdn.net/yimi0903/article/details/11800713 一.安装 Step1:安装MySQL-server,mysql-client 执行以 ...
- iOS 中这些是否熟练掌握——(1)
声明:本篇博文是作者原创作品.参考1 参考2 参考3 参考4 参考5 参考6 关于网上一些关于iOS资料,自己通过学习做了一些整理,这里仅仅作为笔记,方便自己学习使用,加深理解. 1.什么是 ...