[Luogu] 货车运输
https://www.luogu.org/problemnew/show/1967
kruskal + Lca
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string> using namespace std;
const int N = 1e5 + ; #define oo 99999999 struct Node {
int u, v, w;
} S[N * ];
struct Edge {
int u, v, w, nxt;
} E[N << ]; int now = , n, m, js;
int head[N], p[N], f[N][], g[N][], deep[N]; inline int read() {
int x = ;
char c = getchar();
while(c < '' || c > '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} inline bool cmp(Node a, Node b) {
return a.w > b.w;
} int getf(int x) {
return p[x] == x ? x : p[x] = getf(p[x]);
} void add(int u, int v, int w) {
E[now].v = v;
E[now].w = w;
E[now].nxt = head[u];
head[u] = now ++;
} inline void Kruskal() {
for(int i = , doen = ; i <= (m << ) && doen < n; i ++) {
int u = S[i].u, v = S[i].v;
int pu = getf(u), pv = getf(v);
if(pu != pv) {
p[pu] = pv;
add(u, v, S[i].w);
add(v, u, S[i].w);
doen ++;
}
}
} void make_deep(int u, int depth) {
deep[u] = depth;
for(int i = head[u]; ~ i; i = E[i].nxt) {
int v = E[i].v;
if(!deep[v]) {
f[v][] = u;
g[v][] = E[i].w;
make_deep(v, depth + );
}
}
} inline void make_jump() {
for(int j = ; ( << j) <= n; j ++)
for(int i = ; i <= n; i ++)
if(f[i][j - ]) f[i][j] = f[f[i][j - ]][j - ], g[i][j] = min(g[i][j - ], g[f[i][j - ]][j - ]);
} inline int lca(int x, int y) {
int ret = oo;
if(x == y) return ;
if(deep[x] < deep[y]) swap(x, y);
int k = log2(deep[x]);
for(int i = k; i >= ; i --) {
if(deep[f[x][i]] >= deep[y]) {
ret = min(ret, g[x][i]);
x = f[x][i];
}
}
if(x == y) return ret;
for(int i = k; i >= ; i --) {
if(f[x][i] != f[y][i]) {
ret = min(ret, min(g[x][i], g[y][i]));
x = f[x][i];
y = f[y][i];
}
}
ret = min(ret, min(g[x][], g[y][]));
return ret;
} int main() {
n = read();
m = read();
for(int i = ; i <= n; i ++) head[i] = -, p[i] = i;
for(int i = ; i <= m; i ++) S[i].u = read(), S[i].v = read(), S[i].w = read();
sort(S + , S + m + , cmp);
Kruskal();
for(int i = ; i <= n; i ++)
if(!deep[i])
make_deep(i, );//可能会有多块
make_jump();
int T = read();
while(T --) {
int x = read(), y = read();
if(getf(x) != getf(y)) {
printf("-1\n");
continue ;
}
int answer = lca(x, y);
printf("%d\n", answer);
}
return ;
}
[Luogu] 货车运输的更多相关文章
- [Luogu 1967] NOIP2013 货车运输
[Luogu 1967] NOIP2013 货车运输 一年多前令我十分头大的老题终于可以随手切掉了- 然而我这码风又变毒瘤了,我也很绝望. 看着一年前不带类不加空格不空行的清纯码风啊,时光也好像回去了 ...
- [luogu 1967]货车运输
货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情 ...
- kruskal - 倍增 - 并查集 - Luogu 1967 货车运输
P1967 货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过 ...
- Luogu P1967 货车运输(Kruskal重构树)
P1967 货车运输 题面 题目描述 \(A\) 国有 \(n\) 座城市,编号从 \(1\) 到 \(n\) ,城市之间有 \(m\) 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 \ ...
- NOIP 2013 货车运输【Kruskal + 树链剖分 + 线段树 】【倍增】
NOIP 2013 货车运输[树链剖分] 树链剖分 题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在 ...
- NOIP2013 货车运输 (最大生成树+树上倍增LCA)
死磕一道题,中间发现倍增还是掌握的不熟 ,而且深刻理解:SB错误毁一生,憋了近2个小时才调对,不过还好一遍AC省了更多的事,不然我一定会疯掉的... 3287 货车运输 2013年NOIP全国联赛提高 ...
- C++之路进阶——codevs3287(货车运输)
3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description A 国有 n ...
- NOIP2013 货车运输
3.货车运输 (truck.cpp/c/pas) [问题描述] A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货 ...
- Codevs 3287 货车运输 2013年NOIP全国联赛提高组(带权LCA+并查集+最大生成树)
3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description A 国有 n 座 ...
随机推荐
- ACM集训
2019-07-18 09:06:10 emmm.... 昨天5个小时做了一道题,心情复杂,不着急慢慢来 Ivan recently bought a detective book. The book ...
- Java基础IO类之打印流
package IODemo; import java.io.*; /* 打印流 : 很方便的进行输出 字节打印流 增强输出功能 字符打印流 */ public class PrintStreamDe ...
- session和cookie有什么区别?
1.存储位置不同 cookie的数据信息存放在客户端浏览器上. session的数据信息存放在服务器上. 2.存储容量不同 单个cookie保存的数据<=4KB,一个站点最多保存20个Cooki ...
- mouseover mouseleave
mouseover在当鼠标移入元素或其子元素的时候都会触发,是一个重复触发,冒泡的过程.见下面例子,一个父元素包含一个子元素,在其父元素内滑动鼠标,超出子元素的范围时,也会触发事件. 而mouseen ...
- 使用代码浏览WPF控件模版
纯代码创建,不需要创建界面,创建WPF工程后,直接复制代码就可以使用. 当你手头没有Blend,又不记得以下这段代码,但是又想浏览控件模版的时候,就可以直接复制拿来用了. public partial ...
- SQL Server2008分离数据库
1.右击数据库 2.Tasks 3.点击Detach 4.选取Drop Connections-->点击确定 5.开启本地数据库默认存储路径C:\Program Files\Microsoft ...
- webstrom设置语句中的分号
webstrom可以设置语句默认是否添加分号 setting >editor > Code Style > Javascript
- 如何理解H264 编码
H264 结构 https://blog.csdn.net/andywang201001/article/details/80274886 H264 源码 https://www.cnblogs.c ...
- Android NDK 学习之接受Java传入Object数组
本博客主要是在Ubuntu 下开发,且默认你已经安装了Eclipse,Android SDK, Android NDK, CDT插件. 在Eclipse中添加配置NDK,路径如下Eclipse-> ...
- Vue 案例 列表动画实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...